Inheritance Tutorial Java

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.

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.

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.

16 thoughts on “Inheritance Tutorial Java

  1. Tushar

    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. Paranjothi

    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. pavan

    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??

Leave a Reply

Your email address will not be published. Required fields are marked *