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
- The reusability of the code.
- 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.

|