• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"super" question

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ranchers, can someone help me to understand how I can use SUPER to access an overriden method two levels up the class hierarchy? is it even possible to do this. I would like to do something like the following,
class Animal {
public void eat() {
System.out.println("Animal Eat method");
}
}

class Horse extends Animal {
public void eat() {
System.out.println("Animal Eat method");
}
}

public class SuperTest extends Horse{
public static void main(String[] args) {
//i would like to do something like this, but not sure of the syntax
super.super.eat();
}
}

[ June 05, 2008: Message edited by: robert stannard ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


//i would like to do something like this, but not sure of the syntax
super.super.eat()



Basically, based on the way you are attempting it, you can't. If you are inheriting from the horse class, the only way to get to a method of the animal class that has been overridden by the horse class, is through the horse class.

Think of it this way, your class IS-A horse (since it inherits from horse). There is a reason why the horse class overridden the method to make it a horse. If you can by bypass it, then are you still a horse?

Henry
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi buddy ,

with the help of super keyword , you can go up to only one level .

super.goHigh();

but it is not possible ,

super.super.goHigh();

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

I had the same doubt a time ago. Unforttunaly it is only possible to call a super method on the immediate superclass.

Hope helped.
 
Thiago Furuchima
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

I had the same doubt a time ago. Unforttunaly it is only possible to call a super method on the immediate superclass.

Hope helped.
 
Thiago Furuchima
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

I had the same doubt a time ago. Unforttunaly it is only possible to call a super method on the immediate superclass.

Hope helped.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dear robert stannard
Using The Super You can call super class method's or constructor. Here if You want to call Animal class eat method using super keyworh then You call from the Horse Class You Can not Call From the MainClass (SuperTest) like super.super.eat(). i Think It give's Error message.

class Animal {
public void eat() {
System.out.println("Animal Eat method");
}
}
class Horse extends Animal {
public void eat() {
System.out.println("Horse Eat method");
super.eat();
}
}
public class SuperTest extends Horse{
public static void main(String[] args) {
System.out.println("Main Class");
//i would like to do something like this, but not sure of the syntax
super.eat();
}
}

Output Like that::
Main Class
Horse Eat method
Animal Eat method
 
robert stannard
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everyone for your answers and help. I think thats ok now. Makadia I couldnt get your example to work, I think you can only call the Super from a non-static context, I managed to get your example working with this code. Thanks again everyone.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic