my dog learned polymorphism*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inheritance" Watch "inheritance" New topic
Author

inheritance

clyde mel
Greenhorn

Joined: Mar 05, 2002
Posts: 22
How do I access the superclass method using the object of a subclass.
eg:
class a
{
public void add()
{
System.out.println("hello");
}
}
class b extends a
{
public void add()
{
System.out.println("hello1");
}
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
Sheriff

Joined: Aug 26, 2001
Posts: 7610
From JLS 8.4.6.1 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 �15.12.4.9 for discussion and examples of this point.


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

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
clyde mel
Greenhorn

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
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

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
 
Similar Threads
about overloading
help needed on Inner classes program
super . super
array of object to call a method
static variable vs final variable!