Java Reference
In-Depth Information
public class SubClassSuperClassMethods
//Line 1
{
//Line 2
public static void main(String[] args)
//Line 3
{
//Line 4
Rectangle myRectangle1 = new Rectangle();
//Line 5
Rectangle myRectangle2 = new Rectangle(8, 6);
//Line 6
Box myBox1 = new Box();
//Line 7
Box myBox2 = new Box(10, 7, 3);
//Line 8
System.out.println("Line 9: myRectangle1: "
+ myRectangle1);
//Line 9
System.out.println("Line 10: Area of myRectangle1: "
+ myRectangle1.area());
//Line 10
System.out.println("Line 11: myRectangle2: "
+ myRectangle2); //Line 11
System.out.println("Line 12: Area of myRectangle2: "
+ myRectangle2.area());
//Line 12
System.out.println("Line 13: myBox1: " + myBox1); //Line 13
System.out.println("Line 14: Surface Area of myBox1: "
+ myBox1.area());
//Line 14
System.out.println("Line 15: Volume of myBox1: "
+ myBox1.volume());
//Line 15
System.out.println("Line 16: myBox2: " + myBox2); //Line 16
System.out.println("Line 17: Surface Area of myBox2: "
+ myBox2.area());
//Line 17
1
0
System.out.println("Line 18: Volume of myBox2: "
+ myBox2.volume());
//Line 18
}
//Line 19
}
//Line 20
Sample Run:
Line 9: myRectangle1: Length = 0.0; Width = 0.0
Line 10: Area of myRectangle1: 0.0
Line 11: myRectangle2: Length = 8.0; Width = 6.0
Line 12: Area of myRectangle2: 48.0
Line 13: myBox1: Length = 0.0; Width = 0.0; Height = 0.0
Line 14: Surface Area of myBox1: 0.0
Line 15: Volume of myBox1: 0.0
Line 16: myBox2: Length = 10.0; Width = 7.0; Height = 3.0
Line 17: Surface Area of myBox2: 242.0
Line 18: Volume of myBox2: 210.0
The preceding program works as follows: The statement in Line 5 creates the Rectangle
object myRectangle1 and initializes its instance variables to 0 . The statement in Line 6
creates the Rectangle object myRectangle2 and initializes its instance variables length
and width to 8.0 and 6.0 , respectively.
Search WWH ::




Custom Search