Java Reference
In-Depth Information
Next, consider the following application program:
public class SuperSubClassObjects
{
public static void main(String[] args)
{
RectangleShape
rectangle, rectRef;
//Line 1
BoxShape box, boxRef;
//Line 2
rectangle = new RectangleShape(12, 4);
//Line 3
System.out.println("Line 4: Rectangle \n"
+ rectangle+ "\n");
//Line 4
box = new BoxShape(13, 7, 4);
//Line 5
System.out.println("Line 6: Box\n"
+ box+ "\n");
//Line 6
rectRef = box;
//Line 7
System.out.println("Line 8: Box via rectRef\n"
+ rectRef+ "\n");
//Line 8
boxRef = (BoxShape) rectRef;
//Line 9
System.out.println("Line 10: Box via boxRef\n"
+ boxRef + "\n");
//Line 10
if (rectRef instanceof BoxShape)
//Line 11
System.out.println("Line 12: rectRef is "
+ "an instance of BoxShape");
//Line 12
else
//Line 13
System.out.println("Line 14: rectRef is not "
+ "an instance of BoxShape");
//Line 14
if (rectangle instanceof BoxShape)
//Line 15
1
0
System.out.println("Line 16: rectangle is "
+ "an instance of BoxShape");
//Line 16
else
//Line 17
System.out.println("Line 18: rectangle is not "
+ "an instance of BoxShape");
//Line 18
}
}
Sample Run:
Line 4: Rectangle
Length = 12.0, Width = 4.0, Perimeter = 32.0, Area = 48.0
Line 6: Box
Length = 13.0, Width = 7.0, Height = 4.0, Surface Area = 342.0, Volume = 364.0
Line 8: Box via rectRef
Length = 13.0, Width = 7.0, Height = 4.0, Surface Area = 342.0, Volume = 364.0
Search WWH ::




Custom Search