jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inheritance" Watch "inheritance" New topic


clyde mel

Joined: Mar 05, 2002
Posts: 22
How do I access the superclass method using the object of a subclass.
class a
public void add()
class b extends a
public void add()
public static void main(String arg[])
a a1=new a();
b b1=new b();
thru b1 i want to print hello.coz when i call b1.add(),method overriding will take place.Plz help.

Just believe in yourself...
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
From JLS Overriding (by Instance Methods)

An overridden method can be accessed by using a method invocation expression (�15.12) that contains the keyword super. Note that a qualified name or a cast to a superclass type is not effective in attempting to access an overridden method; in this respect, overriding of methods differs from hiding of fields. See � for discussion and examples of this point.

Also have a look at the example shown in JLS Example: Method Invocation using super
Let us know if this is still not clear...
[ June 13, 2002: Message edited by: Valentin Crettaz ]

[Blog] [Blogroll] [My Reviews] My Linked In
clyde mel

Joined: Mar 05, 2002
Posts: 22
I know that the code super.add() is one of the options.But without doing any modification in the code is it possible to print hello thru the object of subclass.State reasons.Thanx
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6264

AFAIK, there's no way to do what you want to do. Why would you want to do that anyway? I can't think of any real-world application for that capability that keeps with the OO way of thinking.
I agree. Here's the link: http://aspose.com/file-tools
subject: inheritance
It's not a secret anymore!