This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

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: 6280

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:
subject: inheritance
jQuery in Action, 3rd edition