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 ]
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 ]
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
Joined: May 17, 2003
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.
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.
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,
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Joined: May 17, 2003
Thanks Satya and Ajith. Your inputs were quite helpful.
subject: Accessing Base class version of overridden method