Inheritance in java with example

Inheritance is one of the concept of the Object- Oriented programming.

Inheritance in java with example

Inheritance in java with example

        

Inheritance is one of the concept of the Object- Oriented programming. The advantage of using the inheritance is

  1. The reusability of the code.
  2. The helps to enhance the properties of the class.

It allows you to define a general class, and later more specialized classes by simply adding some new details. The properties of the parent class will automatically will be inherited by the base class.

The code of the program is given below:

 

<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>

The output of the program is given below:

Download this example.