Home Jsp Simple-jsp-example Inheritance in java with example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Inheritance in java with example

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

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.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 11, 2008

Ask Questions?    Discuss: Inheritance in java with example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Ganesh
April 5, 2011
About inheritance Example

Best Example
kavita kulkarni
April 15, 2011
inheritance in java

Hello, actually i want one example of java (without using jsp,just in normal java script. and would like to see explanation of every step in code.
JONSYL
August 28, 2011
project

nice............
sudarshan
October 13, 2011
should be more explanatory

this could be more explanatory by giving real life example
chaitanya
January 2, 2012
java

hai
DMCA.com