Way2Java

Inheritance Tutorial Java

Composition vs Inheritance

After Encapsulation, the second OOPS feature is "Inheritance". In the previous Parrot program, the Parrot created an object of Bird and used Bird's method eat(). Now in inheritance, we call the eat() method with Parrot object itself (and not with Bird object). This is the difference between composition and inheritance. In composition, same class object is used and in inheritance another class object is used; the another class is known as subclass or derived class. Let us go into the details. Let us revise the earlier Parrot.java program as follows.

class Bird
{
  public void eat()
  {
    System.out.println("All birds eat for their metabolic activities");
  }
}
public class Parrot extends Bird
{
  public static void main(String args[])
  {
    Parrot p1 = new Parrot();
    p1.eat();
  }
}
Output screen of Parrot.java

You observe one new keyword "extends" in the above code; Parrot extends Bird. By extending Bird class, Parrot can create an object of itself and call eat() method of Bird. By extending, Parrot makes a relationship with Bird. This relationship is known as "inheritance". The class which extends is known as subclass (or derived class) and the class getting extended is known as super class (or base class). Bird is called as base class and Parrot is known as derived class. Observe the next program where a subclass uses both composition and inheritance.

Composition and Inheritance

By virtue of inheritance, the subclass object can call super class methods and variables. But remember, the super class cannot call subclass members. In inheritance subclass is very rich because it can make use of its own methods and also super class methods. Following program explains.

class Bird
{ 
  String mouth = "red";
  public void eat()
  {
    System.out.println("All birds eat for their metabolic activities");
  }
}
public class Parrot extends Bird
{
  String body = "green";
  public void food()
  {
    System.out.println("Parrot eats seeds and fruits");
  }
  public static void main(String args[])
  {
    Parrot p1 = new Parrot();
                            // calling its own members with its object
    System.out.println(p1.body);
    p1.food();
                            // calling super class members with its object; inheritance
    System.out.println(p1.mouth);
    p1.eat();
                             // calling super class members with super class object; composition
    Bird b1 = new Bird();
    System.out.println(b1.mouth);
    b1.eat();
  }
}
Output screen of Parrot.java

In the above code, the Parrot class uses both composition (as b1.eat()) and inheritance (as p1.eat()) to use super class members.

Inheritance – "is-a" Relationship

We know earlier, composition uses "has-a" relationship. Now inheritance uses "is-a" relationship. We can feel as, a super class method is a subclass method; that is, the super class eat() method exists as if in the subclass; thereby the subclass object calls the super class method without any hesitation and straightway. This is known as "is-a " relationship. "is-a" relationship is achieved through inheritance.