Inheritance in Java – How Java Inheritance is used

1. Objective

In our previous article, we learned Java Methods. Here, we talk about a new topic called Inheritance in Java – inheritance being a very important pillar in OOP. Along with this, we will cover different types of Java inheritance and how to use them?

So, let’s start Java Inheritance tutorial.

Inheritance in Java

Java Inheritance

2. What is Inheritance in Java?

Java Inheritance is one of the most important features of Object-oriented programming, that makes it very useful. Inheritance in Java, allows us to carry the features of the parent class to the subclasses. This feature saves a lot of time and also data redundancy, as a writer of the codes gets reduced.
Some of the important terms used in Inheritance are –

  • Super Class – A superclass in Java is the class from which the features are inherited.
  • Sub Class – A subclass in Java is the class which inherits the features of the superclass. I can have its own features and methods as well.
  • Reusability – The concept of reusability is supported by inheritance, the codes can be reused by the subclasses.

Do you know about Inner Class in Java Programming?

3. How inheritance used in Java?

The keyword for inheritance is ‘extend’ and the Java syntax is,

class derived-class extends base-class 
{ 
   //methods and fields 
}

Example 

//Program to illustrate the concept of inheritance 
class Shadow
{
    // the Shadow class has two fields
             public int breed;
             public int age;      
    // the Shadow class has one constructor
             public Shadow(int breed, int age)
   	          {
    	             this.breed = breed;
     	             this.age = age;
  	           }    
    // the Shadow class has three methods
              public void trainHello(int decrement)
   	              {
     	                 age -= decrement;
   	              }      
              public void trainSit(int increment)
   	                   {
    	                      age+= increment;
   	                   }
    // toString() method to print info of Shadow
              public String toString()
    	                      {
             	                  return("Breed of shadow "+breed
                                  +"\n"
                                  + "age of shadow is "+age);
   	                      }
}

// derived class

class Medicine extends Shadow
{
   // the Medicine subclass adds one more field
         public int forFur;
   // the Medicine subclass has one constructor
         public Medicine(int breed,int age,
               int presentFur)
    		{
   // invoking base-class(Shadow) constructor
       		 super(breed , age);
       		 forFur = presentFur;
  	        }       
   // the Medicine subclass adds one more method
   	  public void setMedicine(int newValue)
    		      {
       		         forFur = newValue;
    		      }
   // overriding toString() method
    @Override
    	  public String toString()
    		          {
     		              return (super.toString()+
                              "\nseat Medicine is "+forFur);
   		          }
  }

// driver class

public class Test
    {
         public static void main(String args[])
               {
                  Medicine mb = new Medicine(3, 100, 25);
                  System.out.println(mb.toString());
               }
    }

Read about Abstract Class in Java with Example Program

Java Quiz

4. Types of Inheritance in Java

There are five types of Java Inheritance. Let’s discuss them one by one:

a. Single Inheritance in Java

In single inheritance, the features and methods of the superclass are inherited by a single subclass.

Single Inheritance in Java

Single Inheritance

b. Multilevel Inheritance in Java

In multilevel inheritance, the base class inherits the features of the superclass and simultaneously it acts as a superclass for another base class.
Here, B is a base class inheriting the features superclass A and simultaneously it acts a superclass to C.

Multiple Inheritance in Java

Multiple Inheritance

Have you know about Interface in Java

c. Hierarchical Inheritance in Java

In hierarchical inheritance, the features of one superclass are inherited by more than one base class.

Hierarchical Inheritance in Java

Hierarchical Inheritance

d. Multiple Inheritance in Java (Through Interfaces)

In Java, multiple inheritances are only possible with ‘interfaces’, this allows a base class to have more than one superclass.

Multiple Inheritance in Java

Multiple Inheritance

e. Hybrid Inheritance in Java (Through Interfaces)

Java Hybrid inheritance, not support by Java directly, can only via ‘interfaces’ since it is the combination of the two types mentions above.

"<yoastmark

Follow this link to know about Java Data Types

5. Key Facts about Inheritance in Java

  • There can only be one direct superclass to every base class other than for object class.
  • Java Constructors are not members so they are not inherited by any base class.
  • A base class cannot inherit the private members of the parent class.

6. Conclusion

Hence, in this tutorial for Java, we studied the different types of inheritance in java, how to use them and inheritance in java with example program for better understanding. In the future tutorials, we will learn Identifiers in Java Programming Language. Furthermore, if you have any query, feel free to ask in the comment section.

See Also-Java Array

For reference

2 Responses

  1. Ayushi says:

    Thanks for giving these many details on inheritance in Java. I have started my java course online last week. It has got lot of syllabus. Now I have moved to inheritance topic which has objects, inheritance, and types of inheritance. Am finding some topics related to inheritance and java. I got your wonderful article about inheritance and also you have explained types of inheritance with related diagrams. Across your article, the one pictures shows what is inheritance and types of inheritance in java. Excellent summary and looking forward to share with others. Keep updating with the amazing article.

    • DataFlair Team says:

      Hi Ayushi,
      Thank you for such nice feedback on this Java tutorial. We are glad our article helps you in your learnings. We have 100+ Java tutorials, a series of Java interview questions and quizzes in our sidebar you can refer them as well. These interview questions and quizzes help with your practice.
      Keep learning and keep sharing.
      Regards,
      DataFlair

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.