Java Flashcards
What are the three key features of OOP and their advantages?
Encapsulation = Robustness & Reliability. Inheritance = Reuse & Interface Extension. Polymorphism = Implementation Reuse.
What is Encapsulation and it’s advantage using examples?
Encapsulation is to bind related functionality (Methods) & Data (Variables) in a protective wrapper (Class) with required access modifiers (public, private, default & protected). The class then provides a well defined interface to a set of functions to access and manipulate the data. This practice allows for a robust and reliable system where unauthorized access is controlled and internal workings are hidden from it’s users making it easier to maintain.
What is Inheritance and it’s advantage using examples?
Inheritance allows us to derive new classes from existing classes. A subclass inherits all the members (fields, methods, and nested classes) from its superclass. The inherited methods can be overridden with a new implementation or new methods added. The advantage of inheritance is code reuse and interface extension which means that the fields and methods of the existing class can be used without having to re-write and re-debug them.
What is Polymorphism and it’s advantage using examples?
Polymorphism is the ability of an object to take on many forms. For example when a parent class reference is used to refer to a child class object. The advantage of polymorphism is implementation reuse because of the objects ability to morph.
What is an Abstract Class, provide an example?
The purpose of an abstract class is to function as a base for subclasses. It may include both abstract and non-abstract methods, and all types of variables. Abstract classes cannot be instantiated, meaning you cannot create new instances of an abstract class.
What is an Interface, provide an example?
An interface is similar to a class but the body of an interface can include only abstract methods and final fields (constants). Interfaces cannot be instantiated and a class must implement an interface by providing code for each method declared by the interface.
What is an Overloading, provide an example?
Overloading is to add a new method with the same name as an inherited method but with a different signature (Static binding at compile time).
What is an Overriding, provide an example?
Overriding is to redefine an inherited method which have the same signature (dynamic runtime at runtime).
What is composition and how does it differ from inheritance?
Composition is when instances of other classes and primitives are members of the class. This differs to inheritance which holds an “is a” relationship and composition holds an “has a” relationship.
What are the four types of access modifiers?
Public which can be accessed from everywhere in the program, Private from inside the same class, Package from anywhere inside the package and Protected from a subclass and same package that contains the protected member.
What is a checked exception?
Describes a problem that is out of our control (i.e. disk failure) and the compiler handles them at compile time.
What is an unchecked exception?
Describes problems in the programmers code and these are not checked by the compiler.
What is the difference between a checked and unchecked exception?
Checked exceptions must be handled whereas unchecked exceptions may be ignored.
What is parametric polymorphism?
This is another term for Generics.
What is a method signature?
It is the method name and parameter list.
What is auto-boxing and auto-unboxing?
Occurs when a primitive types are converted into their wrapper objects, or converted back into their primitive types. Auto refers to the implicit use of = new Object();
What is a String, StringBuilder and StringBuffer and when is it used?
A String is immutable and using methods on it (i.e. toLowerCase()) returns a new string. Strings should be used for objects that will not change. On the other hand, a StringBuilder is mutable, changing the object rather than returning a new one when changed. StringBuilder should be used when objects will change. StringBuffer is similar to StringBuilder but supports multithreading.
What are the key steps in writing ANY equals() method?
1 - Set parameter to Object obj 2 - Check if object is null 3 - Check if object is an instance of 4 - Cast object into correct class 5 - Check if object is the same instance ( this == obj) 6 - Perform comparisons
What is the difference between Object == Object versus Object.equals(Object)?
Using != and == operator to compare two objects will compare the memory location not the objects’ data. The equals() can be overridden to compare the data…
What is a cast expression?
A cast expression coerces the type into another.
What does “this” refer to?
This refers to the current object (instance).
What is a static method?
A static method is a class level method which requires no object to invoke.
What is an abstract method?
An abstract method needs to be implemented in a subclass.
What is generics and its benefits?
Generics enable types (classes and interfaces) to be parameters when defining classes, methods, and interfaces. This allows for greater code reuse, stronger type checks at compile time, and eliminates the need for type casting.