Inheritance is one of the concept of the Object- Oriented programming. The advantage of using the inheritance is
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:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Inheritance in java with example View All Comments
Post your Comment