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
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:
<title>Working With Constructors and Inheritance</title>
<h1>Working With Constructors and Inheritance</h1>
Rectangle() throws java.io.IOException
Rectangle(int length, int breadth) throws java.io.IOException
this.length = length;
this.breadth = breadth;
area = length*breadth;
pw.println("In Rectangle constructor...<br>");
class Square extends Rectangle
Square(int length, int breadth) throws java.io.IOException
pw.println("In Square constructor...<br>");
pw = out;
Square obj = new Square(10,10);
The output of the program is given below:
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: March 11, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles