aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessing Base class version of overridden method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Accessing Base class version of overridden method" Watch "Accessing Base class version of overridden method" New topic
Author

Accessing Base class version of overridden method

Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
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

Joined: Feb 18, 2004
Posts: 96
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 ]

Rich
SCJP 1.4
Swati Singhal
Ranch Hand

Joined: Dec 08, 2003
Posts: 31
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

Joined: May 17, 2003
Posts: 65
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

Joined: Mar 12, 2004
Posts: 33
U can use super.MethodName(), this will invoke the spuer class version without creating an instance.
Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
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

Joined: Mar 19, 2004
Posts: 8
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

Joined: Mar 17, 2000
Posts: 5782
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,


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
Thanks Satya and Ajith. Your inputs were quite helpful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing Base class version of overridden method