Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Inheritance

Inheritance is the process in which one class inherits the properties of another class.

A class that is inherited is known as a superclass. And the class that inherits superclass is known as a subclass.

The superclass can also be known as parent-class and subclass as child-class.

Now let’s explain it by giving real world example.

Let’s take an example of human beings.

Consider, a man inherits some physical properties from his parents.
If the father or mother of a man is tall, then he might be tall.
If the father or mother of a man is fair, then he might be fair with the addition of his own physical properties as well, like he might have curly hairs in spite of his parents not having curly hairs.

A man can inherit not only physical properties from his parents but also a wealth of his parents. If his father has a house automatically a man will own his father’s house and he might have his own car too. And if this man has a son, his son will own house of his grandfather as well as the car of his father, and a son might have his own bike too.

It can be explained more easily through picture shown below:

Java Programming Language Inheritance Explanation

Similarly, in Java, one class can inherit properties of another class and can have its own properties too. That is, subclass/child-class can inherit properties of its superclass/parent-class, as well as it can contain its own properties.

Advantages of Inheritance

1) Code reusability.
2) Code enhancement – Each subclass can use its own classification.
3) Run time polymorphism can be achieved.

Code reusability

Once the code is written in a superclass (parent class) there is no need to write it again in a subclass (child class) because a subclass automatically inherits the code written in a superclass.

To inherit a superclass into a subclass, we just need to use extends keyword.

It’s like that once a father takes the desired Car, why should son buy the same Car because he too can use the same Car.

Code enhancement

The subclass can use code inherited from superclass as well as it can have its own code too.

It’s like that a son can use the Car of a father as well as he has the freedom to buy his own Car too.

Run time polymorphism can be achieved

Runtime polymorphism can be achieved with the use of method overriding in inheritance.

Syntax for inheritance

 class Superclass
{
    // fields and methods of superclass
}

 class Subclass extends Superclass
{
    // fields and methods of subclass
}

The extends keyword indicates that the subclass (Child class) is inheriting properties of superclass (Parent class).

Simple Inheritance Program

class Parent 
{
    String h = "House";
}

class Child extends Parent
{
    String v = "Car";
 	
    public void childHas()
    {
        System.out.println("Child has "+h+" as well as "+v);
    }
}

class InheritanceDemo 
{
    public static void main(String srgs[])
    {
        Child c = new Child();
        c.childHas();
    }
}

Output

Child has House as well as Car

Here in the above program, you may feel that Child class contains only one field/instance variable String v = “Car”. But in actual, Child class also inherits/contains instance variable String h = “House” from Parent Class.

Therefore in real, the Child class is as shown below:

class Child extends Parent
{
    String h = "House"; // inherited from Parent class
    String v = "Car";
	
    public void childHas()
    {
	 System.out.println("Child has "+h+" as well as "+v);
    }
}

Example – II

class Parent
{
    String h = "house";
	
    public void parentProperty() 
    {
        System.out.println("Property :: "+h);
    }
}

class Child extends Parent
{
    String v = "Car";
	
    public void childProperty() 
    {
        System.out.println("Property :: "+v);
    }
}

class InheritanceDemo2 
{ 
    public static void main(String args[])
    {
        Parent p = new Parent();
        Child c = new Child();
		
        System.out.println("Parent propery :: ");
        p.parentProperty(); // invoked by Parent, so used to display Parent members
        System.out.println("------------------------");
        System.out.println("Child propery :: ");
        c.parentProperty(); // invoked by Child, so used to display Child members
        c.childProperty();	
    }
}

Output

Parent propery ::
Property :: house
------------------------
Child propery ::
Property :: house
Property :: Car

Example – III

class Parent
{
    int x;
	
    public void assignParent(int x) 
    {
        this.x = x;
    }
	
    public void displayParenVariable() 
    {
        System.out.println("x :: "+x);	
    }
}

class Child extends Parent
{
    int y;
	
    public void assignChild(int y) 
    {
        this.y = y;
    }
	
    public void displayChildVariable() 
    {
        System.out.println("y :: "+y);
    }
}

class InheritanceDemo3 
{
    public static void main(String args[]) 
    {
        Parent p = new Parent();
        Child c = new Child();
		
        p.assignParent(10); // invoked by Parent, so used to assign Parent members
        c.assignParent(20); // invoked by Child, so used to assign Child members
        c.assignChild(30);
		
        System.out.println("Display Parent variables");
        p.displayParenVariable();// invoked by Parent, so used to display Parent
                                                                             members
        System.out.println("--------------------------");
        System.out.println("Display Child variables");
        c.displayParenVariable(); // invoked by Child, so used to display Child
                                                                             members
        c.displayChildVariable();
    }
}

Output

Display Parent variables
x :: 10
--------------------------
Display Child variables
x :: 20
y :: 30

Note

In inheritance, both subclass and superclass can work completely independent without depending on each other with their own objects.

 

Leave a reply