Composition or IS-A relationship between two classes
An IS-A signifies a relationship between the two classes that are connected to each other through inheritance. The basis of an IS-A relationship is -
When a class extends another concrete(non-abstract) class.
When a class extends an abstract class.
When a class implements an interface.
An IS-A relationship when a class extends another concrete(non-abstract) class.
Here, a concrete class Fruit is extended by specialized class Grapes. Hence, we can say that Grapes class IS-A type of Fruit.
class Fruit
{
public void color()
{
System.out.println("Fruit has a color");
}
}
class Grapes extends Fruit
{
public void color()
{
System.out.println("The color of grapes could be green or black.");
}
}
An IS-A relationship when a class extends another abstract class.
Here, a general abstract class Vegetable is extended by specialized class Tomato. This is why we say that Tomato class is in a IS-A relationship with class Vegetable.
abstract Vegetable
{
public void shape()
{
System.out.println("Vegetable has shape");
}
public abstract void color();
}
class Tomato extends Vegetable
{
public void shape()
{
System.out.println("Tomato is round");
}
public void color()
{
System.out.println("The color of Tomato is red");
}
}
Non-abstract methods of abstract class Vegetable, such as shape() is inherited and overridden by Tomato class.
Abstract method of abstract class, such as color() is implemented by class Tomato.
Hence, class Tomato class has features of abstract class Vegetable and this is why Tomato IS-A type of Vegetable.
An IS-A relationship when a class implements an interface.
Here, an interface Sports is implements by a concrete class Tennis. Hence, we can say that Tennis IS-A type of Sports.
interface Sports
{
void play();
boolean isTeamSport();
boolean isAnIndvidualSport();
}
class Tennis implements Sports
{
public void play()
{
System.out.println("Let's play Tennis!");
}
public boolean isTeamSport()
{
return false;
}
public boolean isAnIndividualSport()
{
return true;
}
}
In the preceding code, skeletal methods of an interface, Sports, are inherited and implemented by a class, Tennis.
Hence, class Tennis has features of interface Sports and this is why it is known to have an IS-A
relationship with Sports because i.e. Tennis IS-A type of Sports.
Advertisement
Aggregation or HAS-A relationship between two classes.
When class A has a reference to the object of another class B, class A is said to be in a HAS-A relationship with class B.
Through this reference of class B in it, class A can call and use the needed features of class B, without copying all the unnecessary code of class B in it.
Let's understand the concept of HAS-A with an example -
class Math
{
public int doAdd(int a, int b)
{
return (a+b);
}
public int doSubtract(int a, int b)
{
return (a-b);
}
}
class FindTotal
{
Math ob = new Math(); //FindTotal class HAS-A Math reference to a Math object
public static void main(String... ar)
{
FindTotal total= new FindTotal();
int sum= total.ob.doAdd(2,3); //Using Math reference to call method doAdd() of Math class
System.out.println("Sum is : "+ sum);
int difference= total.ob.doSubtract(5,3);//Using Math reference to call method doSubtract() of Math class
System.out.println("Difference is "+ difference);
}
}
Output-
Sum is : 5
Difference is 2
Program Analysis
A reference of Math class is contained in FindTotal class.
The class FindTotal HAS-A reference of Math class in it, which points to the object of Math class.
FindTotal class has used this reference of Math class, to call the methods of Math class - doAdd() and doSubtract().
Hence, the FindTotal class has used the features(methods) of Math class through the reference of Math class,
without needing to duplicate the methods of Math class within FindTotal class.