Home Jsp Simple-jsp-example Inheritance in java with example



Inheritance in java with example
Posted on: March 11, 2008 at 12:00 AM
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.

Related Tags for Inheritance in java with example:
cprogramminginheritanceclassantpropertiesobjectusabilityclasseshelpgeneralnewtagusingaiparentdefineiecalladdprogramtoautomaticramtailicalbasesseciwsbiautoeilitlslipeimceincaladdinaddingasmntparpsmincaddminjadadvantageclesspecialspecreusabilitydetailsautomaticallyabiaseallagemeobjprodetaildetailtiesspsoatplylateishallomamplproperandarcodcodertropesasimusavazssrirenthconceptabatieufineujeicaicaplpropprmindodeonomogro


More Tutorials from this section

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

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.