Tutorials

Object Oriented Programming


Java Inheritance


Inheritance is the property by virtue of which a class acquires or inherits the property of another class.

The class which inherits the property is called sub-class.

 

The class whose property is acquired or inherited is known as super class.

 

If class A wants to inherit the property of class B then write:

 

Class B{}    //super class

Class A extends B{}   //A is sub class of B

 

extends in java

In java extends keyword helps to achieve inheritance.

 

When sub class extends the super class then it can inherit the property of super class such as:

  • State of super class (variables, etc)
  • Behavior of super class (methods)

Restrictions: If super class has marked the variable or methods as private or final then those will not be inherited by subclass.

 

Types of Inheritance

  • Single inheritance

  • Multi level inheritance

  • Hierarchal inheritance

  • Multiple inheritance

  • Hybrid inheritance

 

Single Inheritance

The class A extends the class B this is the example of single inheritance.

Find another example in diagram given below:

 

                        Single Inheritance by tutorialsinhand.com    

 

Example: Above diagram is explained through code snippet.

 

Source: SingleLevel.java


package inheritance;

public class SingleLevel {

      public static void main(String[] args) {

            Student student = new Student();
            System.out.println("Student name: "+ student.name);
            System.out.println("College name: "+ student.collegeName);
            System.out.println("College code: "+ student.collegeCode);

            student.facility();

      }
}

Source: College.java


class College{

      String collegeName="GNIT";

      int collegeCode=1234;



      public void facility(){

            System.out.println("Digital classroom with expert teachers");

      }

}

Source: Student.java


class Student extends College{

      String name = "Rajneesh";

}

Multilevel Inheritance

When a class A extends class B and class B extends class C then multilevel inheritance is formed.

An example is explained in the diagram given below:

 

                         Mulitilevel Inheritance by tutorialsinhand.com

 

Example: Above diagram is explained through code snippet.

 

Source:SingleLevel.java


package inheritance;

public class SingleLevel {

      public static void main(String[] args) {

            Student student = new Student();

            System.out.println("Student name: "+ student.name);

            System.out.println("College name: "+ student.collegeName);

            System.out.println("College code: "+ student.collegeCode);

            System.out.println("fees: "+ student.fees);



            student.facility();

      }

}

Source: fees.java


class fees{

      int fees = 30000;

}

Source: College.java


class College extends fees{

      String collegeName="GNIT";

      int collegeCode=1234;



      public void facility(){

            System.out.println("Digital classroom with expert teachers");

      }

}

Source: Student.java


class Student extends College{

      String name = "Rajneesh";

}

 

Hierarchical Inheritance

When class A and class B both extends class C then hierarchical inheritance is formed.

There can be more classes like D, E, F etc. extending class C and that would also be an example of hierarchical inheritance.

An example is explained in the diagram given below:

 

          hierarchical inheritance by tutorialsinhand.com

 

Example: Above diagram is explained through code snippet.

 

Source: heirarchicalInheritance.java


package inheritance;



public class hierarchicalInheritance {



      public static void main(String[] args) {

            Students student = new Students();

            student.course();

            String name = student.name;

            int amount = student.feePaid;

            student.paidOrReceived(name, amount);

      }

}

Source: fee.java


class fee{

      int fees = 30000;

      //this fee can be teaching fee or students fee

      public void paidOrReceived(String name, int amt){

            System.out.println(name+ " paid or received Rs"+ amt);

      }

}

Source: Teacher.java


class Teacher extends fee{

      String teacherName="Rajneesh";

      int teacherCode=12345;

     

      public void teaches(){

            System.out.println("Computer Science");

      }

}

Source: Students.java

 


class Students extends fee{

      String name = "Mukesh";
      int feePaid = 29000;
     
      public static void course(){

            System.out.println("Course: IT");

      }

}

OUTPUT:

Course: IT

Mukesh paid or received Rs29000

 

Inheritance not supported in java

Java does not support multiple inheritance

Since multiple inheritance is not supported so hybrid will automatically not be supported in java as it includes multiple inheritance.

 

Multiple Inheritance

Consider a scenario where class A tries to extend Class B, Class C and few more classes. Such scenarios results in multiple inheritance.

See an example in below diagram where class Admission is trying to extend both Science class and Commerce class:

 

           multiple inheritance by tutorialsinhand.com

 

 

Why java doesn’t support multiple inheritance?

If a single class inherits more than one class then there is chances of arising ambiguity. 

Suppose two classes which are inherited by any third class (or child class) has method with same name. If the method is called from child class then which classes method should be called is not clear in this case. So this creates a ambiguous scenario.

 

For example consider above example from multiple inheritance. 

Suppose class Science and class Commerce both has method "formFillUp()".

Admission class inherits both the class Science and class Commerce.

 

Now if we try to call or use "formFillUp()method from within Admission class then we have a question arising for us.

 

Which class "formFillUp()method is called?

Since by the property of inheritance class Admission has access to both classes method so it will be difficult for it to decide which one to call. 

This is the ambiguity we were indicating to so far.

To help reduce the chances of such situations arising java doesnot support multiple inheritance.

 

Interface can help acheives multiple inheritance indirectly. Read more 

 

Hybrid Inheritance

Hybrid Inheritance is the combination of the multiple and multilevel inheritance.

Since multiple inheritance is not supported so it makes clear that hybrid will not get supported by java.



Please Share this page
Views : 93