This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Inheritence: Method Overriding question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritence: Method Overriding question" Watch "Inheritence: Method Overriding question" New topic
Author

Inheritence: Method Overriding question

Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
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

Joined: Nov 08, 2007
Posts: 457
PostRealCode


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
Originally posted by Bill Shirley:
PostRealCode


Didn't have any.. i just wrote the code to explain my doubts.
gylph knor
Greenhorn

Joined: Jun 12, 2008
Posts: 20
polymorphism is applied on mthods only ,
variables are not polynorphic
Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
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

Joined: Apr 20, 2006
Posts: 3344

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!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
 
subject: Inheritence: Method Overriding question
 
Similar Threads
How do I get the Method and Class caller?
Method Overriding doubt
Scope and classes/methods
Overriding static methods in subclass