Home Jsp Simple-jsp-example Constructor Inheritance
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Constructor Inheritance

Advertisement
Constructors are used to create objects from the class. Constructor declaration are just like method declaration, except that they do not have any return type and they use the name of the class.

Constructor Inheritance

        

Constructors are used to create objects from the class. Constructor declaration are just like method declaration, except that they do not have any return type and they use the name of the class. The compiler provides us with a default constructor to the class having no arguments.

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>Working With Constructors and Inheritance</title>
  </head>
<body>
  <h1>Working With Constructors and Inheritance</h1>
  <%!
  javax.servlet.jsp.JspWriter pw;
  class Rectangle
  {
int length;
int breadth;
int area;
  Rectangle() throws java.io.IOException 
  {
  pw.println("In constructor...<br>");
  }
  Rectangle(int length, int breadth)  throws java.io.IOException
  {
  this.length = length;
  this.breadth = breadth;
  area = length*breadth;
  pw.println("In Rectangle constructor...<br>");
  pw.println(area);
     pw.println("<br>");
  }
  }  
  class Square extends Rectangle  
  {
  Square(int length, int breadth) throws java.io.IOException 
  {
  super(length, breadth);
  pw.println("In Square constructor...<br>");
  pw.println(area);
  }
  }
  %> 
  <%
  pw = out; 
  Square obj = new Square(10,10);
  %>
  </body>
</html>

The output of the program is given below:

Download this example.

Advertisements

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: Constructor Inheritance  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com