Inheritance in Java

Inheritance, one of the important concepts of Object Oriented Programming (OOPS) defines a general class and establishes a subtype from an existing object. What are the Benefit of using inheritance. Inheritance allows access of properties and methods of super class by a sub class. Extend keyword is used to inherit all the properties of the super-classes by subclasses.

Inheritance in Java

Inheritance, one of the important concepts of Object Oriented Programming (OOPS) defines a general class and establishes a subtype from an existing object. What are the Benefit of using inheritance. Inheritance allows access of properties and methods of super class by a sub class. Extend keyword is used to inherit all the properties of the super-classes by subclasses.

Inheritance in Java


Inheritance, one of the important concepts of Object Oriented Programming (OOPS) defines a general class and establishes a subtype from an existing object.

Benefit of using inheritance:

  • A code can be used again and again
  • Inheritance in Java enhances the properties of the class, which means that property of the parent class will automatically be inherited by the base class
  • It can define more specialized classes by adding new details.

Inheritance allows access of properties and methods of super class by a sub class. Extend keyword is used to inherit all the properties of the superclasses by subclasses. (Subclass is a class derived from another class)

There are two types of inheritance in Java:

  1. Simple Inheritance
  2. Multilevel Inheritance

Simple Inheritance:

Single inheritance or One-level inheritance is when a subclass is derived from parent class. There is only a sub class and it's parent class.

Multilevel Inheritance:

Multiple Inheritance is when a subclass is derived from a derived class. The derived class is called the subclass or child class for it's parent class and this parent class works as the child class for it's just above (parent) class.

Multilevel inheritance can go up to any number of level.

* Though Java does not support Multiple Inheritance it can be achieved by using the interface and implementing more than one interfaces in a class.

Example of Inheritance in Java:

<html>
    <head>
        <title>How to Use Inheritance in the  jsp</title>
    </head>
        <body>
        <h1>How to Use Inheritance in the  jsp</h1>
        <%!
            javax.servlet.jsp.JspWriter pw;
            class Rectangle
            {
                                int length =10;
                                int breadth =20 ;
                public int areaOfRectangle() throws java.io.IOException
                {
                    pw.println("Calculating the area of a rectangle...<br>");
                                        return(length*breadth);
                }
            }
            class Square extends Rectangle
            {
                public int areaOfSquare() throws java.io.IOException
                {
                    pw.println("Calculating the area of a square...<br>");
                                        return(length*length);
                }
            }
        %>    
        <%
            pw = out;    
            Square sq = new Square();
            int area1 = sq.areaOfRectangle();
                        out.println(area1);
                        out.println("<br>");
            int area2 = sq.areaOfSquare();
                        out.println(area2);
        %>
    </body>
</html>