Monday, January 3, 2011

Class & Objects..2

Superclass References and Subclass Objects
As you know, Java is a strongly typed language. Aside from the standard conversions and automatic promotions that apply to its primitive types, type compatibility is strictly enforced. Therefore, a reference variable for one class type cannot normally refer to an object of another class type. For example, consider the following program.

// This will not compile.
class X {
int a;
X(int i) { a = i; }
}
class Y {
int a;
Y(int i) { a = i; }
}
class IncompatibleRef {
public static void main(String args[]) {
X x = new X(10);
X x2;
Y y = new Y(5);
x2 = x; // OK, both of same type
x2 = y; // Error, not of same type
}
}
Here, even though class X and class Y are physically the same, it is not possible to assign an X reference to a Y object because they have different types. In general, an object reference variable can refer only to objects of its type. There is, however, an important exception to Java’s strict type enforcement. A reference
variable of a superclass can be assigned a reference to any subclass derived from that superclass. Here is an example:

// A superclass reference can refer to a subclass object.
class X {
int a;
X(int i) { a = i; }
}
class Y extends X {
int b;
Y(int i, int j) {
super( j );
b = i;
}
}
class SupSubRef {
public static void main(String args[]) {
X x = new X(10);
X x2;
Y y = new Y(5, 6);
x2 = x; // OK, both of same type
System.out.println("x2.a: " + x2.a);
x2 = y; // still Ok because Y is derived from X
System.out.println("x2.a: " + x2.a);
// X references know only about X members
x2.a = 19; // OK
// x2.b = 27; // Error, X doesn't have a b member
}
}

Here, Y is now derived from X; thus it is permissible for x2 to be assigned a reference to a Y object. It is important to understand that it is the type of the reference variable— not the type of the object that it refers to—that determines what members can be accessed. That is, when a reference to a subclass object is assigned to a superclass reference variable, you will have access only to those parts of the object defined by the superclass. This is why x2 can’t access b even when it refers to a Y object. If you think about it, this makes sense, because the superclass has no knowledge of what a subclass adds to it. This is why the last line of code in the program is commented out. Although the preceding discussion may seem a bit esoteric, it has some important practical applications. One is described here. The other is discussed later in this module, when method overriding is covered.
An important place where subclass references are assigned to superclass variables is when constructors are called in a class hierarchy. As you know, it is common for a class to define a constructor that takes an object of the class as a parameter. This allows the class to construct a copy of an object. Subclasses of such a class can take advantage of this feature. For example, consider the following versions of TwoDShape and Triangle. Both add constructors that take
an object as a parameter.

class TwoDShape {
private double width;
private double height;
// A default constructor.
TwoDShape() {
width = height = 0.0;
}
// Parameterized constructor.
TwoDShape(double w, double h) {
width = w;
height = h;
}
// Construct object with equal width and height.
TwoDShape(double x) {
width = height = x;
}
// Construct an object from an object.
TwoDShape(TwoDShape ob) {
width = ob.width;
height = ob.height;
}
// Accessor methods for width and height.
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
void showDim() {
System.out.println("Width and height are " +
width + " and " + height);
}
}
// A subclass of TwoDShape for triangles.
class Triangle extends TwoDShape {
private String style;
// A default constructor.
Triangle() {
super();
style = "null";
}
// Constructor for Triangle.
Triangle(String s, double w, double h) {
super(w, h); // call superclass constructor
style = s;
}
// Construct an isosceles triangle.
Triangle(double x) {
super(x); // call superclass constructor
style = "isosceles";
}
// Construct an object from an object.
Triangle(Triangle ob) {
super(ob); // pass object to TwoDShape constructor
style = ob.style;
}
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
System.out.println("Triangle is " + style);
}
}
class Shapes7 {
public static void main(String args[]) {
Triangle t1 =
new Triangle("right", 8.0, 12.0);
// make a copy of t1
Triangle t2 = new Triangle(t1);
System.out.println("Info for t1: ");
t1.showStyle();
t1.showDim();
System.out.println("Area is " + t1.area());
System.out.println();
System.out.println("Info for t2: ");
t2.showStyle();
t2.showDim();
System.out.println("Area is " + t2.area());
}
}

In this program, t2 is constructed from t1 and is, thus, identical. The output is shown here.
Info for t1:
Triangle is right
Width and height are 8.0 and 12.0
Area is 48.0
Info for t2:
Triangle is right
Width and height are 8.0 and 12.0
Area is 48.0
Pay special attention to this Triangle constructor:

// Construct an object from an object.
Triangle(Triangle ob) {
super(ob); // pass object to TwoDShape constructor
style = ob.style;
}
It receives an object of type Triangle and it passes that object (through super) to this TwoDShape constructor:

// Construct an object from an object.
TwoDShape(TwoDShape ob) {
width = ob.width;
height = ob.height;
}

The key point is that TwoDshape( ) is expecting a TwoDShape object. However, Triangle( ) passes it a Triangle object. The reason this works is because, as explained, a superclass reference can refer to a subclass object. Thus it is perfectly acceptable to pass TwoDShape( ) a reference to an object of a class derived from TwoDShape. Because the TwoDShape( ) constructor is initializing only those portions of the subclass object that are members of TwoDShape, it doesn’t matter that the object might also contain other
members added by derived classes.