OOP
1. What is OOP? Briefly Explain Encapsulation, Inheritance, and Polymorphism.
- Programming paradigm that abstracts real-word entities into objects.
- Objects have attributes (called fields or properties) and behaviors (called methods).
-
Encapsulation
- Bundling an object's data and methods into a single unit, hiding the internal details from the outside world.
- Interaction occurs only through the interfaces provided by the object.
-
Inheritance
- Allows a subclass to share the data structures and methods of a parent class.
- A key means of code reuse.
- Helps establish a hierarchical relationship between classes, making the structure clearer.
-
Polymorphism
- Object can have multiple forms.
- Polymorphism can be divided into compile-time polymorphism (overloading) and runtime polymorphism (overriding).
2. Where is Polymorphism Reflected?
-
Method Overloading
- Allows a class to have multiple methods with the same name but different parameter lists (differing in parameter types, number, or order).
- The compiler determines which method to call at compile time based on the passed parameters.
- Example: An
add
method can be defined asadd(int a, int b)
andadd(double a, double b).
-
Method Overriding
- Allows a subclass to provide a specific implementation for a method that is already defined in its parent class.
- At runtime, the JVM determines which version of the method to call based on the actual type of the object.
- Example: In an
Animal
class, asound
method is defined; the subclassDog
can override it to implementbark
, whileCat
can implementmeow
.
- Interfaces and Implementations
- Multiple classes can implement the same interface.
- References of the interface type can be used to call methods of these classes.
- Example:
Animal
interface withsound()
method,Dog
andCat
classes implementingsound()
.
- Upcasting and Downcasting
- A parent class-type reference can point to a subclass object, which is known as upcasting.
- Enables the use of different subclass implementations at runtime.
- Downcasting: Converting a parent class reference back to its subclass type.
- Require verifying the actual object type to avoid
ClassCastException
.
3. Differences Between Abstract Class and Regular Class
- Instantiation: A regular class can be directly instantiated into an object, whereas an abstract class cannot be instantiated and can only be inherited.
- Method Implementation: Methods in a regular class can have specific implementations, while methods in an abstract class may or may not have implementations.
- Inheritance: A class can inherit from one regular class and implement multiple interfaces; however, a class can only inherit from one abstract class but can implement multiple interfaces simultaneously.
- Implementation Constraints: A regular class can be inherited and used by other classes, whereas an abstract class is typically used as a base class, intended to be inherited and extended by other classes.
- The subclass must implement all the abstract methods of the abstract class.
4. What is the difference between Abstract class and Interface?
-
Characteristics of Both:
-
Abstract Class
- Used to describe the common characteristics and behaviors of a class.
- can have member variables, constructors, and concrete methods.
-
Interface
- Used to define behavioral specifications.
- Supports multiple implementations.
- Can contain constants, abstract methods, default methods, and static methods. (Two last two since Java 8)
-
-
Differences Between the Two:
- Implementation Approach
- Implementing Interface:
implements
- Inheriting Abstract Class:
extends
- A class can implement multiple interfaces, but it can only inherit one abstract class.
- Use interfaces can indirectly achieve multiple inheritance.
- Implementing Interface:
- Access Modifiers
- In an interface, member variables are implicitly
public static final
, must be initialized, and cannot be modified. - All member methods are
public
- In abstract class, member variables and member methods are implicitly
public abstract
.
- In an interface, member variables are implicitly
- Variables
- Abstract class can contain instance variables and static variables.
- Interface can only contain constants (i.e., static constants).
- Implementation Approach
5. What methods can be defined inside an interface?
- Abstract method
- All classes implementing the interface must provide implementations for these methods.
- Abstract methods are implicitly
public abstract
.
public interface Animal {
void makeSound();
} - Default method
- Introduced in Java 8.
- Allow interfaces to provide concrete implementations.
- Implementing classes can choose to override these default methods.
public interface Animal {
void makeSound();
default void sleep() {
System.out.println("Sleeping...");
}
} - Static method
- Introduced in Java 8.
- Can be called directly using the interface name, without requiring an instance of an implementing class.
public interface Animal {
void makeSound();
static void staticMethod() {
System.out.println("Static method in interface");
}
} - Private method
- Introduced in Java 9.
- Provide helper functionality for default methods or other private methods within the interface.
public interface Animal {
void makeSound();
default void sleep() {
System.out.println("Sleeping...");
logSleep();
}
private void logSleep() {
System.out.println("Logging sleep");
}
}
6. Can an abstract class be instantiated?
- Cannotbe instantiated directly.
- The primary purpose of an abstract class is to be inherited.
- An abstract class can have constructors, which are called during the instantiation of a subclass to perform necessary initialization.
public abstract class AbstractClass {
public AbstractClass() {
// Constructor code
}
public abstract void abstractMethod();
}
public class ConcreteClass extends AbstractClass {
public ConcreteClass() {
super(); // Calls the abstract class's constructor
}
@Override
public void abstractMethod() {
// Implementation of the abstract method
}
}
// The following code can run
ConcreteClass obj = new ConcreteClass();
- When we create an instance of
ConcreteClass
, the constructor ofAbstractClass
is called. However, this does not meanAbstractClass
is instantiated.