Type of Relation in java.
In java basically we have two type of relation i.e. IS-A relation and HAS-A relation.
Using inheritance concept we can achieve IS-A relation and by using Association we can achieve HAS-A relation in java.
IS-A relation IS-A relation is a tightly coupled relation means that if we modify super class property sub class property will also automatically modifies. Whenever we extends the one class from the another class that concept is knowns as IS-A relation.
WE can use IS-A relation if one class is a specialized version of another class. Ex. class Animal { void eat() { System.out.println(“Generic animal is eating”); } }
class Dog extend Animal() { //IS-A relation void bark() { System.out.println(“ the dog is barks”); } } public class Test { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); dog.bark(); } }
HAS-A Relation
We can achieve HAS-A relation with the help of Association concept in association we have two type Composition and Aggregation.
Composition
Composition is known as strong Relation in java is it also known as tightly coupled relation means that one object can’t live without another object and one object is destroyed another object will not work. For Ex1 : Me and my friend is tightly coupled relation like i make chapatis and my friend makes curry so we are depends and one another. like without chapatis there is no use of curry and without curry there is no use of chapatis.
Ex2: Car and engine is a strong relation means that without car there is no use of engine and without engine there is no use of car both are strongly depends on each other.
Aggregation
Aggregation is known as weak relation means one object and exist independently without another object. It same as composition but it as weaker as compare to composition. In this if one object is destroyed another object is still exist.
For Ex1: Car and Music player car has its own appearance and music player has its own appearance. Both can be work without each other. If we destroy Music player it will not affect the car and if we destroy car it will not affect the music player both can still exist.
Ex2": Student and College College has its own appearance and student has its own appearance both can exits without one another. If we destroy one of the object it will not affect the another object still the another will exist.