Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing Base class version of overridden method

 
Gopal Shah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS Pg 177, Sec. 8.4.6, 3rd Para
===============
a qualified name or a cast to a superclass type is not effective in attempting to access an overridden method
===============
Why does Java not provide a way to invoke the Base class version of overridden method ?

c++ provides a way, by having the option of passing by value or passing by reference.
Edited by Corey McGlone: Preserved Formatting
[ March 25, 2004: Message edited by: Corey McGlone ]
 
Richard Quist
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would guess that the rationale is something like this:
Since you (or the subclass implementor) have chosen to override the base class method in the first place, then you really didn't want users of your subclass to call the base class method directly...presumably your subclass method is providing extra value/functionality that is necessary for safe/proper operation.
If you really wanted to provide access to the base class method without going through the subclass method that overrides it you could always provide a different subclass method that calls the base class method....(but then I'd have to wonder why the method was overridden in the first place....)

Just my 2 cents...
[ March 23, 2004: Message edited by: Richard Quist ]
 
Swati Singhal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To access the base class version of an overridden method, you need to create an object of the parents type. For example, consider the following code:

In the above code, at line 1, you are creating an object of the parent class, hence the overridden method of the parent gets called.
However, if you create an object of the child class, as in line 2, the method in child class will get called.
Hope this helps
 
Gopal Shah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Richard. But what I meant to say is as follows:
C++ Prog. Lang, by Stroustrup, Pg.360

If I want to invoke the method of actual object I would pass the reference as in the code above.
But suppose I want to invoke the base class version of debug_print() irrespective of the kind of expression thrown, I would have the catch clause as

=========================
Richard u r right in saying that implementer had chosen to override the base class method for some purpose.
But just look at the example in the code. The exception of Matherr was caught as the default case. I don't want to look at the particulars of exactly which kind of exception it was, just it should print the Base class version of debug_print().
There r cases when u receive an object from some Factory. U really don't know what is the exact class of it (whether it is Base class object or one of the Derived class object). Since u don't know the class names of all the Derived classes, u can't check with instanceof operator.
In such cases what is the solution ? Basically, I want to suppress polymorphism.
 
Deepa Guha
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U can use super.MethodName(), this will invoke the spuer class version without creating an instance.
 
Gopal Shah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deepa Guha:
U can use super.MethodName(), this will invoke the spuer class version without creating an instance.

Deepa I don't think its the right solution.
Note: U can use the keyword "super" inside the class definition of the derived class, to refer to base class members. Not outside it.
 
Satya Kondur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gopal
In C++ this happens due to object slicing. When you pass ploymorphic objects by value they get sliced. Since you are left only with the base class part you see the base class version of the method getting invoked.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gopal Shah:

There r cases when u receive an object from some Factory. U really don't know what is the exact class of it (whether it is Base class object or one of the Derived class object). Since u don't know the class names of all the Derived classes, u can't check with instanceof operator.
In such cases what is the solution ? Basically, I want to suppress polymorphism.

You can use some of the methods provided by java.lang.Class to inspect the inheritance. For instance,
BaseClass.class.isAssignableFrom(derivedObj.class) to see if the object derivedObj is an object of any subtype of BaseClass. However you will not be able to "supress polymorphism" ie., you will not be able to invoke an overridden method using a subclass instance.

Hope that helps,
 
Gopal Shah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Satya and Ajith. Your inputs were quite helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic