Inheritance -


The process of using (inheriting) properties of existing class into new class is known as inheritance. In such case, the previously existed class is known as super/parent/base class and new class is known as subclass/derived/child class.
Java supports the concept of reusability by inheritance. It is always good if you can reuse something that existed previously. Through inheritance new classes can reuse the variables and methods of the previously existed classes.

extends keyword in java -

A subclass can inherit the properties of super class by using the keyword extends.
For example -
class subclass extends superclass
{
  // variables and methods declarations
}
The subclass will now has its own as well as super class variables and methods if they are not private. i.e. private members do not inherit.

Types of Inheritance -

Java supports the following types of inheritance -
1- Single level inheritance
2- Multi level inheritance
3- Hierarchical inheritance
4- Multiple Inheritance (using interface)
5- Hybrid Inheritance


1. Single level inheritance -

In single level inheritance there is one subclass inheriting the properties of one super class. i.e. there is single level of inheritance.
single level inheritance

Example -

Below is the example of single level inheritance, where Student class (class B) is inheriting the Marks class (class A).
class Marks
{
    private int phy, chem;
    
    public void inputM()
    {
        phy  = Integer.parseInt(System.console().readLine("Enter physics marks"));
        chem  = Integer.parseInt(System.console().readLine("Enter chemistry marks"));
    }
    public void dispM()
    {
        System.out.println(phy+"  "+chem);
    }
    public Marks()
    {
        System.out.println("Marks Default");
    }
    public Marks(int p)
    {
        phy = p;
    }
    public Marks(int p, int c)
    {
        phy = p;
        chem = c;
    }
}

class Student extends Marks
{
    private int roll;
    private String name;
    
    public void inputS()
    {
        roll = Integer.parseInt(System.console().readLine("Enter roll"));
        name = System.console().readLine();
        super.inputM();
    }
    public void dispS()
    {
        System.out.println(roll+ "  "+ name);
        super.dispM();
    }
    public Student()
    {
        System.out.println("Student Default");
    }
    public Student(int r)
    {
        roll = r;
    }
    public Student(int r, String n)
    {
        roll = r;
        name = n;
    }
    public Student(int r, String n, int p, int c)
    {
        super(p, c);
        roll = r;
        name = n;
    }
    
}

public class Demo
{
    public static void main(String[] args) 
    {
        Student s1 = new Student();
    }
}
    
        

Output -

            Marks Default
            Student Default

        

Program Desciption -

Program defines a super class named Marks that is inheriting by sub class named Student. So Student class, in addition to its own variables and methods, also has Marks class variables and methods (which are not private, as we already know private members cannot be inherited).
In fact in our example Student class will not have any variable of Marks class because all variables of Marks class are private. Only methods of Marks class will be available there.
Note the use of super keyword within methods of Student class.

super keyword -

super is used to call the super class methods and constructors within subclass.
Always remember that constructors do not inherit. We can only call them in sub class constructors using super keyword. You should remember the following points while calling parent class constructor -
1- super() should be the first statement within constructor. Where as whithin methods this is not the compulsion, super can be anywhere.
2- super() should match the order and type of variables passed in the constructor that we want to call. Now consider output of the program. When the statement
Student s1 = new Student();
gets executed , we already know it is going to call default constructor of Student class. And always remember that if we have not defined any super() within any constructor in case of subclass, then there will be a hidden super() that make call to parent class default constructor. i.e.
public Student()
 {
   super();     // this statement will be hidden
   System.out.println("Student Default");
 } 
When super() will get executed, compiler will look for a constructor that is not having any parameters (i.e. default constructor) and that constructor gets executed first and print 'Marks Default' on output screen, then it comes back and print 'Student Default'.
Now consider the second variation of above program, we are going to change only main class (Demo) as follows -
public class Demo
{
    public static void main(String[] args) 
    {
        Student s1 = new Student(1, "John", 80, 90);
         s1.dispS();
    }
}         
     

Output -

         1     John
         80    90
     
In this case when the statement
Student s1 = new Student(1, "John", 80, 90);
gets executed, compiler will look for a constructor in Student class that is having 4 parameters, first one of integer type, second one of String type, third and fourth are of integer types. It finds it there and executes it.
The first statement in constructor is super(p, c); it causes the compiler to look for a constructor in super class (Marks) for a constructor that is having two int type parameters and execute that constructor. This constructor will set the value of phy and chem variables at 80 and 90. Then it comes back to Student constructor and set the values of roll and name to 1 and John respectively.
The statement s1.disp(); gets executed and causes the dispS() method of Student class to call. This will first print the value of roll and name and then call dispM() method of Marks class and that will print the value of phy and chem.

Multilevel Inheritance

Multilevel inheritance involve more than two classes. In multilevel inheritance a sub class will be inheriting the properties of its super class and that super class will be inheriting the properties of its super class and so on.
multilevel inheritance

Example -

In the below example class A will be Marks class, class B will be Student class and class C will be Comment class.

class Marks
{
    private int phy, chem;
    
    public void inputM()
    {
        phy  = Integer.parseInt(System.console().readLine("Enter physics marks"));
        chem  = Integer.parseInt(System.console().readLine("Enter chemistry marks"));
    }
    public void dispM()
    {
        System.out.println(phy+"  "+chem);
    }
    public Marks()
    {
        System.out.println("Marks Default");
    }
    public Marks(int p)
    {
        phy = p;
    }
    public Marks(int p, int c)
    {
        phy = p;
        chem = c;
    }
}

class Student extends Marks
{
    private int roll;
    private String name;
    
    public void inputS()
    {
        roll = Integer.parseInt(System.console().readLine("Enter roll"));
        name = System.console().readLine();
        super.inputM();
    }
    public void dispS()
    {
        System.out.println(roll+ "  "+ name);
        super.dispM();
    }
    public Student()
    {
        System.out.println("Student Default");
    }
    public Student(int r)
    {
        roll = r;
    }
    public Student(int r, String n)
    {
        roll = r;
        name = n;
    }
    public Student(int r, String n, int p, int c)
    {
        super(p, c);
        roll = r;
        name = n;
    }
    
}

class Comment extends Student
{
    String comment;
    public void inputC()
    {
        super.inputS();
        comment = System.console().readLine("Enter Comments");
    }
    public void dispC()
    {
        super.dispS();
        System.out.println(comment);
    }
    
    public Comment()
    {
        System.out.println("Comment Default");
    }
    public Comment(int r, String n, int p, int c, String com)
    {
        super(r, n, p, c);
        comment = com;
    }
    
}
public class Demo
{
    public static void main(String[] args) 
    {
        Comment c1 = new Comment(1, "John", 80, 90, "Good");
        c1.dispC();
    }  
}
         
     

Output

         1    John
         80   90
         Good
     


Hierarchical Inheritance -

In hierarchical inheritance there is single parent class that is inheriting by multiple subclasses. It is somewhat similar to tree based structure.
hierarchical inheritance

Example -

In the below example class A will be Marks class, class B will be Student class and class C will be Comment class.
class Marks
{
    private int phy, chem;
    
    public void inputM()
    {
        phy  = Integer.parseInt(System.console().readLine("Enter physics marks"));
        chem  = Integer.parseInt(System.console().readLine("Enter chemistry marks"));
    }
    public void dispM()
    {
        System.out.println(phy+"  "+chem);
    }
    public Marks()
    {
        System.out.println("Marks Default");
    }
    public Marks(int p)
    {
        phy = p;
    }
    public Marks(int p, int c)
    {
        phy = p;
        chem = c;
    }
}

class Student extends Marks
{
    private int roll;
    private String name;
    
    public void inputS()
    {
        roll = Integer.parseInt(System.console().readLine("Enter roll"));
        name = System.console().readLine();
        super.inputM();
    }
    public void dispS()
    {
        System.out.println(roll+ "  "+ name);
        super.dispM();
    }
    public Student()
    {
        System.out.println("Student Default");
    }
    public Student(int r)
    {
        roll = r;
    }
    public Student(int r, String n)
    {
        roll = r;
        name = n;
    }
    public Student(int r, String n, int p, int c)
    {
        super(p, c);
        roll = r;
        name = n;
    }
    
}

class Comment extends Marks
{
    String comment;
    public void inputC()
    {
        super.inputM();  // Here we can not call Student class Methods
        comment = System.console().readLine("Enter Comments");
    }
    public void dispC()
    {
        super.dispM();
        System.out.println(comment);
    }
    
    public Comment()
    {
        System.out.println("Comment Default");
    }
    public Comment(int p, int c, String com)
    {
        super(p, c);
        comment = com;
    }
    
}
public class Demo
{
    public static void main(String[] args) 
    {
        Student s1  = new Student(1, "John", 80, 90);
        s1.dispS();
        Comment c1 = new Comment(100, 70, "Good");
        c1.dispC();
    }
}
              
          

Output -

              1     John
              80    90
              100    70
              Good
          
Here both Student and Comment classes are inheriting Marks class. So in Comment class we cannot call Student class constructor or methods using super because there is no relation between them. That?s why we make changes in Comment class constructors and methods.

Multiple and Hybrid Inheritance -

Multiple inheritance is just reverse of Hierarchical inheritance. That is, there is one sub class inheriting the properties of multiple super classes.
multiple inheritance
Hybrid inheritance is a combination of two or more inheritance, for example combination of hierarchical inheritance and multiple inheritance.
hybrid inheritance
Multiple and Hybrid inheritance are not supported by java classes. Java supports them using the concept of interface. So we will learn them in chapter of interface.

Method Overriding (Function Overloading)-

In case of inheritance, When a method in the subclass has the same name and type signature as method in the super class, then method in subclass is said to be overridden method and this process is known as method overriding.

Example -

class First
{
    private int a;
    public First(int a)
    {
        this.a = a;
    }
    public void disp()
    {
        System.out.println("a = "+a);
    }
}

class Second extends First
{
    int x,y;
    public Second(int x, int y, int a)
    {
        super(a);
        this.x = x;
        this.y = y;
    }
    
    public void disp()  // Overridden method
    {
        System.out.println("x and y = "+x+"   "+y);
    }
}
  class Demo
  {
      public static void main(String[] args)
      {
          Second s1 = new Second(1, 2, 3);
          s1.disp();  // call overridden method
          
          First f1 = new First(1);
          f1.disp();   // call First class disp() method
      }
  }
                              
                          

Output -

     x and y = 1   2
     a = 1