1. What is Inheritance in OOP?
Inheritance is a mechanism where a new class (subclass) inherits properties and behaviors (methods) from an existing class (superclass). This relationship is often described as an "is-a" relationship. For example, a Car is a type of Vehicle, meaning that Car can inherit common properties and behaviors from the Vehicle class.
1.1 How Does Inheritance Work?
In Java, inheritance is implemented using the extends keyword. The subclass inherits all non-private fields and methods from the superclass, allowing for code reuse and logical hierarchies.
Example:
class Vehicle {
String brand;
int speed;
void accelerate() {
speed += 10;
}
}
class Car extends Vehicle {
int numberOfDoors;
void displayInfo() {
System.out.println("Brand: " + brand + ", Speed: " + speed + ", Doors: " + numberOfDoors);
}
}
In this example, Car inherits brand and speed fields, as well as the accelerate() method from the Vehicle class.
1.2 Advantages of Inheritance
- Code Reusability : Common functionalities can be written once in the superclass and reused by multiple subclasses.
- Logical Hierarchy : Helps create a natural hierarchy that models real-world relationships.
1.3 Disadvantages of Inheritance
- Tight Coupling : Subclasses are tightly coupled with their superclasses, making the code harder to modify and extend.
- Fragile Base Class Problem : Changes in the superclass may unintentionally affect the behavior of subclasses.
2. What is Composition in OOP?
Composition involves building complex types by combining objects of other types, typically through class fields. It is often described as a "has-a" relationship. For example, a Car has an Engine, meaning that the Car class will have an instance of the Engine class.
2.1 How Does Composition Work?
In composition, a class is composed of one or more objects of other classes, rather than inheriting from them. This promotes flexibility and reduces dependency between classes.
Example:
class Engine {
int horsepower;
void start() {
System.out.println("Engine started with " + horsepower + " HP.");
}
}
class Car {
Engine engine;
Car(Engine engine) {
this.engine = engine;
}
void startCar() {
engine.start();
}
}
In this example, Car has an Engine, and it can start the engine through composition rather than inheritance.
2.2 Advantages of Composition
- Flexibility : Composition allows changing or updating components without affecting the system.
- Loose Coupling : Components are loosely coupled, which promotes code modularity and easier testing.
2.3 Disadvantages of Composition
- More Boilerplate Code : Composition might require writing more code compared to inheritance, especially in simple scenarios.
- Potentially Higher Complexity : Managing multiple objects can add complexity to the codebase.
3. Inheritance vs. Composition: When to Use Which?
3.1 Choosing Inheritance
Read more at : Methods to Differentiate Between Inheritance and Composition in Object-Oriented Programming