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();
}
}

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();
}
}

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.
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?
Sir..plz tell me super class object will be created when subclass object is created…??
It is not created immediately unless you use any super class members.
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.
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??
Your choice, which ever have main(), it executes and the other do not. If both have main, both can execute. Try.
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
Sir how can i extend multiple classes with single extend keyword is it possible??
No possible.
sir i loved this website. its awesome simply superb mind blowing work. thank u so much for u support
sir simply superb sir.. i liked this website.. its awesome
Sir,
Can u explain aggregation……….
See the following link of this same web site.
http://way2java.com/oops-concepts/aggregation-and-composition/
Sir, we can like this in the above program
parrot p1=new bird();
p1.eat();
Not possible as you are assigning super class anonymous object to subclass object. It requires explicit casting. The other way is possible.
Simply Great work…!
sir then why we use private in a class to show encapsulation. please explain it in detail