• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in assigning super class and subclass variables

 
vinayak manda
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
vinayak manda
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred ,That was very helpful,and mind boggling concept.
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic