In this article is a relationship in java we give the information about The “IS-A” relationship signifies that a subclass is a specialized version of its superclass, and it inherits the properties and behaviors of the superclass.
IS-A relationship in Java:
The “IS-A” relationship signifies that a subclass is a specialized version of its superclass, and it inherits the properties and behaviors of the superclass.
When we say that a subclass “is-a” superclass, we mean that the subclass is a specific type of the superclass.
If you have a superclass called “Vehicle,” you can create subclasses like “Car” and “Bicycle.” In this case, you can say that a “Car is-a Vehicle” and a “Bicycle is-a Vehicle.”
IS-A relationship in between Base-Class and Derived-Class
By using this relationship, we add the property and behavior of one class to another class without modifying the class. It is also known as Parent-Child Relationship.
This type of relationship is achieved by extends keyword.
Here extends keyword is creating a unidirectional bridge from Parent Class to Child Class.
There are 2 type of IS-A relationships exist in Java:
- Class Inheritance (extends keyword is used)
- Interface Inheritance (implements keyword is used)
Usage of IS-A relationship in Java:
- The main usage of IS-A relationship among Java classes is Code Reusability.
- Another usage of IS-A relationship is Overriding (Method Overriding).
- Method Overriding (Run-Time Polymorphism)
- Method Overloading (Compile-Time Polymorphism)
- In Method Overriding, the Child Class provides an implementation of a method which is already declared and defined in Parent Class
Advantages & Disadvantages of IS-A Relationship
Advantages of IS-A Relationship:
- Reusability of our code.
- Provides a certain level of abstraction.
- We can extend new functionalities by simply extending the current class and provide new features in child class.
Disadvantages of IS-A Relationship:
- Our child class is tightly coupled with our parent class. Any changes in parent class is going to affect our child class.
- There is a loss of flexibility due to tight coupling.
HAS-A relationship in Java:
- Sometimes, we don’t need all the data variables and methods of our parent class.
- In that case we use, a special type of relationship named as HAS-A relationship.
- HAS-A relationship shows us the relation between two or more classes by connecting them with their objects.
- There is no specific keyword for HAS-A relationship, although we are going to use “new” keyword often in case of HAS-A relationship.
- It is also known as ASSOCIATION because here we are associating two or more classes through their specific reference variables & objects.
- There are two forms of ASSOCIATION which are known as COMPOSITION & AGGREGATION.
What is a Aggregation?
- It is a type of HAS-A relationship in which the bonding or association between Parent Class and Child Class is Weak
- Both classes (Parent Class and their Child Classes) are not strongly dependent on each other.
- In aggregation, one entity (objects) can exist without other entity. It is a unidirectional association.
For example, a Cat can have an engine but vice versa is not possible. This unidirectional design pattern.