aspose file tools*
The moose likes Beginning Java and the fly likes Doubt in assigning super class and subclass variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in assigning super class and subclass variables " Watch "Doubt in assigning super class and subclass variables " New topic
Author

Doubt in assigning super class and subclass variables

vinayak manda
Ranch Hand

Joined: Jul 27, 2004
Posts: 31
class A {}
class B extends A {B b = new B(); A a = new A(); A a = b; }

As the above classes give an example of superclass type variable containing a reference to a subclass object.

I read that subclass objects can be treated as superclass objects.And that a superclass type variable that contains a reference to a subclass object calls the subclass method. //I got this point

But plz make me understand this point:

Assigning a subclass object's reference to a superclass type variable is safe,because the subclass object is an object of its superclass. However,this reference can be used to invoke only superclass methods.If the code refers to only subclass only members through the superclass variable,the compiler reports errors.

However ,this reference can be used to invoke only superclass methods ,,,what does this mean
superclass type variable that contains a reference to a subclass object should call the subclass method right ...I hope I'am clear in explaining
Kalai Selvan
Ranch Hand

Joined: Jul 07, 2004
Posts: 79
Hi Vinayak,
When an Super Class ref. points to the SubClass Object, you can access only those methods which are inhereted from the super class. Since super class ref. knows only methods declared or defined in the super class.

bye,
Kalai Selvan.
vinayak manda
Ranch Hand

Joined: Jul 27, 2004
Posts: 31
Hi Selvan,

Thanks for the reply,

If you don't mind ,can you please show me a code snippet comparing mine and your statement.That would be very helpful.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

If i understand what you're asking, what we have is this...


ok. now i make


we all agree that b can call method1, method2 and method3. all are legal, and method2 will get our overridden version.

what can we do with a? since a is an A-type object, it a ONLY KNOWS about method1 and method2. you cannot call method3 using a. but when you call method2, you should get the correct overridden version defined in B.

Both references (a and b) point to the same object in memory. But the a-reference doesn't know that a method3 exists, so the compiler will complain.

at least, i THINK that's how it works.
[ July 27, 2004: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
vinayak manda
Ranch Hand

Joined: Jul 27, 2004
Posts: 31
Thanks Fred ,That was very helpful,and mind boggling concept.
Sadanand Murthy
Ranch Hand

Joined: Nov 26, 2003
Posts: 382
Fred gave a very good explanation.

Keep in mind that there is concept called early-binding. In early binding, the compiler knows what type of object a variable is. In Fred's example, the compiler knows that var 'a' is an instance of A; so early binding occurs and the compiler will complain on a.method3().

At run time what happens is that 'a' is actually a reference which is pointing to an object that is of type B. So, calling a.method2() basically results in the method2() getting executed of the object that 'a' is referencing, which in this example is B. So, the overridden method2() gets invoked.


Ever Existing, Ever Conscious, Ever-new Bliss
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt in assigning super class and subclass variables