|
|
|
public class myProg
{
public static void main(String[] args)
{
GeometricObject a;
Circle b = new Circle("red", 1);
a = b; // Upcasting
System.out.println(a.getArea());
}
}
|
public class GeometricObject
{
private String color;
GeometricObject( String col ) // Constructor
{
color = col;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getArea() // Dummy method !!
{
return 0; // Some default value
}
}
|
public class Circle extends GeometricObject
{
private double radius;
Circle(String col, double r)
{
super(col);
radius = r;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return 3.14159*radius*radius;
}
public String toString()
{
return "Color = " + getColor() + ":"
+ "radius = " + radius;
}
}
|
public class Rectangle extends GeometricObject
{
private double width;
private double height;
Rectangle(String col, double w, double h)
{
super(col); width = w; height = h;
}
public double getWidth() { return width;}
public void setWidth(double r) { width = r; }
public double getHeight() { return height;}
public void setHeight(double r) { height = r; }
public double getArea()
{
return width*height;
}
public String toString()
{
return "Color = " + getColor() + ":"
+ "width = " + width
+ " height = " + height;
}
}
|
|
|
|
public class myProg
{
public static void main(String[] args)
{
GeometricObject a
= new GeometricObject("red");
Circle b;
b = a; // Downcasting
// Compile error !! (Illegal !)
}
}
|
public class GeometricObject
{
private String color;
GeometricObject( String col ) // Constructor
{
color = col;
}
public String getColor() { return color; }
public void setColor(String c) { color = c; }
public double getArea() // Dummy method !!
{
return 0; // Some default value
}
}
|
public class Circle extends GeometricObject
{
private double radius;
Circle(String col, double r)
{
super(col);
radius = r;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return 3.14159*radius*radius;
}
public String toString()
{
return "Color = " + getColor() + ":"
+ "radius = " + radius;
}
}
|
public class Rectangle extends GeometricObject
{
private double width;
private double height;
Rectangle(String col, double w, double h)
{
super(col); width = w; height = h;
}
public double getWidth() { return width;}
public void setWidth(double r) { width = r; }
public double getHeight() { return height;}
public void setHeight(double r) { height = r; }
public double getArea()
{
return width*height;
}
public String toString()
{
return "Color = " + getColor() + ":"
+ "width = " + width
+ " height = " + height;
}
}
|
|
|
|
|
|
|
|
We can perform the correct downcast operation based on the subclass type of the object referenced by a:
public static void main(String[] args)
{
GeometricObject a = (can be a Circle or Rectangle object)
// Upcasting
if ( a is a Circle )
{
print its radius
}
else if ( a is a Rectangle )
{
print its width and height
}
else
print "invalid subclass type"
}
|
If a references to a Circle object, then downcast (convert) to a Circle variable:
public static void main(String[] args)
{
GeometricObject a = (can be a Circle or Rectangle object)
// Upcasting
if ( a instanceof Circle )
{
Circle b = (Circle) a; // Downcast to a Circle
System.out.println(b.getRadius());
}
else if ( a is a Rectangle )
{
print its width and height
}
else
print "invalid subclass type"
}
|
If a references to a Rectangle object, then downcast (convert) to a Rectangle variable:
public static void main(String[] args)
{
GeometricObject a = (can be a Circle or Rectangle object)
// Upcasting
if ( a instanceof Circle )
{
Circle b = (Circle) a; // Downcast to a Circle
System.out.println(b.getRadius());
}
else if ( a instanceof Rectangle )
{
Rectangle b = (Rectangle) a; // Downcast to a Rectangle
System.out.println(b.getWidth());
System.out.println(b.getHeight());
}
else
print "invalid subclass type"
}
|
Otherwise (it can be a GeometricObject !), print the warning message:
public static void main(String[] args)
{
GeometricObject a = (can be a Circle or Rectangle object)
// Upcasting
if ( a instanceof Circle )
{
Circle b = (Circle) a; // Downcast to a Circle
System.out.println(b.getRadius());
}
else if ( a instanceof Rectangle )
{
Rectangle b = (Rectangle) a; // Downcast to a Rectangle
System.out.println(b.getWidth());
System.out.println(b.getHeight());
}
else
System.out.println("Invalid subclass type");
}
|
Demo