Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritence: Method Overriding question

 
Anupam Bhatt
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Again,

I have developed this basic doubt and need some light over it...

Consider scenario:



The output i see is:
Hello from Class B
Hello from Class A
Class B
Class B < ------ have the doubt here


My doubt is, when we can access the original value for dummyStr [i.e the value assigned in the super class] by typecasting the ClassB instance to ClassA, why can't we access the implementation for the method in ClassA via typecast?

I understand i can get hold of the super class implementation for the method via the super keyword, but want to understand, why is the reference to ClassA obtained by typecasting, not executing the impl in ClassA.
[ June 24, 2008: Message edited by: Anupam Bhatt ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables are not polymorphic. You can't "override" a variable in a superclass, you can only hide it. By casting to the parent type, you can effectively "move the subclass variable out of the way," so you can see the hidden copy in the parent. Which variable you get depends on the type of the reference to the object, and it's chosen at the time the program is compiled.

Methods are polymorphic. That means that the JVM decides which method to call while the program is running. The choice is made based on the actual class of the object you call the method on. There's no way to "move the subclass method out of the way" -- it just doesn't work like that. There's no way for an outside class to call the parent version of the method.

Check out this story to understand more -- I guarantee it will help.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming the following is coded in the ClassB class.



ClassB is a subtype of ClassA. Even though the instance of ClassB can be referenced as a ClassA, the this operator points to the actual concrete class not the reference type. So, in your example, since you are calling this.returnString(), you are calling the implementation coded in ClassB.

The fact that you are storing a reference to an instance of ClassB as a ClassA does not change the semantics of the this operator.

Creating a ClassA reference variable to an instance of ClassB and casting the ClassB object to populate the reference does not change the object. A ClassB object will always be a ClassB object...forever. Casting it, storing a reference variable to it with a different permissible type does not change this. Never.

P.S. This is a awkward way to code anyway. Much better to program with composition and interfaces.
[ June 24, 2008: Message edited by: James Clark ]
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PostRealCode
 
Anupam Bhatt
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bill Shirley:
PostRealCode


Didn't have any.. i just wrote the code to explain my doubts.
 
gylph knor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
polymorphism is applied on mthods only ,
variables are not polynorphic
 
Anupam Bhatt
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all,

I think, the answer to my question was in:

"Which variable you get depends on the type of the reference to the object, and it's chosen at the time the program is compiled."

and

"The fact that you are storing a reference to an instance of ClassB as a ClassA does not change the semantics of the this operator."


Thanks again.
[ June 25, 2008: Message edited by: Anupam Bhatt ]
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anupam Bhatt:


"The fact that you are storing a reference to an instance of ClassB as a ClassA does not change the semantics of the this operator."




That's correct. It is because the methods are NOT polymorphic. So no matter what the methods are invoked based on the 'actual object's type' and not its reference type!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic