Problem 1:
class Shape {
int noOfShape;
Shape() //Constructor
{
System.out.println(“Shape constructor”);
}
Shape(int s) //Constructor
{
noOfShape = s;
System.out.println(“Shape=“+noOfShape);
}
}
class Rectangle extends Shape {
int noOfRectangle;
Rectangle() // Constructor
{
super(99);
System.out.println(“Rectangleconstructor”);
}
Rectangle(int r) //Constructor
{
noOfRectangle = r;
System.out.println(“Rectangle =“+noOfRectangle);
}
}
class Box extends Rectangle {
int noOfBox;
Box(int b) // Constructor
{
noOfBox = b;
System.out.println(“Box =“+noOfBox);
}
}
class testMain {
public static void main(String arg[])
{
Box b1 = new Box(10);
Rectangle r1 = new Rectangle(20);
Shape s1 = new Shape(30);
Rectangle r2 = new Rectangle();
Shape s2 = new Shape();
}
}
Questions:
Question1. Define the term constructor. Using the above Java program explain constructor chaining.
Question2. Explain the declaration: Rectangle r1 = new Rectangle (20).
Question3. What is the meaning of keywords super and extends?
Question4. What is the output of above Java program?