• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance

 
clyde mel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
clyde mel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic