A class Shape is defined with two overloading constructors in it. Another class Test1 is partially defined which inherits the class Shape. The class Test1 should include two overloading constructors as appropriate for some object instantiation shown in main() method. You should define the constructors using the super class constructors. Also, override the method calculate( ) in Test1 to calculate the volume of a Shape.
Program :
import java.util.Scanner;
class Shape{
double length, breadth;
Shape(double l, double b){ //Constructor to initialize a Shape object
length = l;
breadth= b;
}
Shape(double len){ //Constructor to initialize another Shape object
length = breadth = len;
}
double calculate(){// To calculate the area of a shape object
return length * breadth ;
}
}
public class Test1 extends Shape{
double height;
Test1(double length,double h) {
//base class constructor with one parameter is called
super(length);
height=h;
}
Test1(double length,double breadth,double h) {
//base class constructor having two argument is called
super(length,breadth);
height=h;
}
double calculate() {
return length*breadth*height;
}
Input | Output | |
Test Case 1 | 2.0 3.0 4.0 | 16.0 24.0 |
No comments:
Post a Comment