70 likes | 147 Views
Inheritance(3). Superclass references and subclass objects. A reference variable of a superclass can be assigned a reference to an object of any subclass derived from that superclass . In other words, a superclass reference can refer to a subclass object.
E N D
Superclass references and subclass objects • A reference variable of a superclass can be assigned a reference to an object of any subclass derived from that superclass. • In other words, a superclass reference can refer to a subclass object. • Remember that a subclass “is-a” superclass, so a subclass object is asuperclass object. • Conversely, a reference variable of a subclass cannot be assigned a reference to an object of its superclass. Java Programming
Example of superclass references and subclass objects class X { int a; X(int par) { a = par; } } class Y extends X { int b; Y(int par) { super(par); b = par; } } class Demo { public static void main(String… args) { X objX1 = new X(5), objX2; Y objY1 = new Y(10), objY2; objX2 = objX1; System.out.println(objX2.a); // print 5 objX2 = objY1; //legal since Y is a subclass of X System.out.println(objX2.a); // print 10 // objX2.b = 100; // illegal } } Java Programming
Notes on Superclass references and subclass objects • The type of the reference variable — not the type of the object that it refers to — determines what members can be accessed. • 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. • So, objX2.b is illegal. Java Programming
Example of casting class X { int a; X(int par) { a = par; } } class Y extends X { int b; Y(int par) { super(par); b = par; } } class Demo { public static void main(String… args) { X objX1 = new Y(10, 10); //legal Y objY1; // objY1 = objX1; // illegal objY1 = (Y)objX1; // legal by casting // objX1.b = 100; // illegal ((Y)objX1).b = 100; // legal objX1 = new X(5); objY1 = (Y)objX1; // legal but an exception occurs } } Java Programming
Compatibility of class types • Java allows an object to be referred through a variable declared with one of the parent class type of the object’s class. • The following example shows one of the usage of polymorphism in which the meaning of objA depends on the context of the code. • Assume that C class extends B class and B class extends A class. A objA; B objB; C objC; objA = new A( ); objA = new B( ); objA = new C( ); objB = new A( ); // illegal objB = new B( ); objB = new C( ); objC = new A( ); // illegal objC = new B( ); // illegal objC = new C( ); • In the above example, through objAonly the parts of the object that are parts of class A can be accessed and the parts of the object that are parts of the real object’s class are hidden, same for objB. Language Design
Accessing overridden fields • Example • Assume that class B inherits from class A, intdABis defined in A and B as public and int dB is defined only in B as public . A objA = new A(); B objB = new B(); objA.dAB = 5; // legal, no matter what the real object is, referred to field dAB in A objB.dAB = 5; // legal, no matter what the real object is, referred to field dAB in B objA.dB = 5; // illegal no matter what the real object is, dB not defined in A objB.dB = 5; // legal, no matter what the real object is, referred to field dB in B Language Design