About Lesson
Introduction:
-
The HAS-A relationship in Java is called Composition. By using a “has-a” relationship we ensure code reusability in our programs.
-
We make our program use an instance of a class directly instead of extending it from another class as we do in the case of inheritance.
-
For example, a car and an Engine share the HAS-A relationship. A car always has an Engine. We do not extend the properties of the Engine object, but we use the Engine object directly.
-
The composition is an association in which one class contains another class, and this contained class depends on the containing class in such a way that it cannot exist independently.
-
Hence, when the containing object is destroyed, the other object is also destroyed.
-
A composition relationship is a “part-of-a-whole” relationship in which the part does not exist without the whole.
Code:
class Engine {
public void startEngine() {
System.out.println("Car Engine Started.");
}
public void stopEngine() {
System.out.println("Car Engine Stopped.");
}
}
class Car {
private String color;
private int max_Speed;
public void carDetails(){
System.out.println("Color= "+color + " Max Speed= " +
}
public void setColor(String color) {
this.color = color;
}
public void setMaxSpeed(int max_Speed) {
this.max_Speed = max_Speed;
}
}
class Civic extends Car {
public void CivicStart(){
Engine engine = new Engine(); //composition
engine.startEngine();
}
}
public class Main {
public static void main(String[] args) {
Civic civic = new Civic();
civic.setColor("Silver");
civic.setMaxSpeed(180);
civic.carDetails();
civic.CivicStart();
}
}
Why Composition over Inheritance?
One of the main reasons to prefer composition over inheritance is to promote code reuse and maintainability.
Here are some advantages of using composition:
-
Flexibility: With composition, classes can be composed of multiple objects of different types, allowing for more flexible and dynamic behaviour. This allows for greater adaptability and extensibility in the code base.
-
Loose coupling: Composition promotes loose coupling between classes as they interact through interfaces or contracts, rather than relying on direct inheritance relationships. This reduces the dependencies between classes and makes the code base more modular and easier to maintain.
-
Encapsulation: Composed objects can be encapsulated within a class, hiding their internal details and providing a clean and well-structured API. This improves the overall design and reduces the risk of exposing unnecessary implementation details.
-
Code organisation: Composition allows for better code organisation by breaking down complex functionality into smaller, more manageable components. This improves code readability and makes it easier to understand and maintain.
-
Avoiding the limitations of multiple inheritance: Inheritance can become complex and error-prone when dealing with multiple inheritance. Composition, on the other hand, allows for combining behaviour from multiple classes without the complexities and limitations of multiple inheritance.
Overall, composition provides a more flexible and maintainable approach to designing classes, promoting code reuse, loose coupling, encapsulation, and better code organisation.