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();
  }
}
Inheritance Tutorial Java
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();
  }
}
Inheritance Tutorial Java
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.

17 thoughts on “Inheritance Tutorial Java”

  1. Hi sir..can i know if we inherit superclass and we dont use superclass method in subclass and we call directly superclass method with subclass object what will happen? Is it nessesary that we have to override superclass method in subclass in inheritance?

  2. Hello Sir,

    This site is awesome for java learners through internet,. I’m having small doubt over relationship concept in java, As i know there are 3

    1. has-a relation,
    2. is-a relation,
    3. use-a relation.

    I got good answers for “has-a” and “is-a” relation through “composition and inheritence”, Could please tell me what is “use-a relation” ?, What does it mean ?, Where exactly is used?

    Please reply ASAP.

  3. If my file contains more than one class i can save that file with the class name which is having ‘public’ access specifier and coming to main method in which class i need to create main method??

      1. Haii sir , if both classes in same package contains the main methods then from where the JVM starts executing .

        FOR EXAMPLE –>

        package demo;
        public class test1{
        public static void main(String[] args){
        System.out.println(“test1 main is printed “);
        }
        }
        package demo;
        public class test2 {
        public static void main(String[] args){
        System.out.println(“test2 main is printed “);
        }

        To which class (test1/ test2) JVM gives the priority

Leave a Comment

Your email address will not be published.