• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

how to use super  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to call methods from the top of a class hierarchy using super?

Example:
class C1 {
public void method() {
System.out.println("C1");
}
}
class C2 extends C1 {
public void method() {
super.method();
System.out.println("C2");
}
}
class C3 extends C2 {
public void method() {
// can I also call the method in C1 here
}
}
Can I also call the method in C1 inside the method in C3?
Thanks.

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Cherry Sta. Romana:
Is it possible to call methods from the top of a class hierarchy using super?

Example:
class C1 {
public void method() {
System.out.println("C1");
}
}
class C2 extends C1 {
public void method() {
super.method();
System.out.println("C2");
}
}
class C3 extends C2 {
public void method() {
// can I also call the method in C1 here
}
}
Can I also call the method in C1 inside the method in C3?
Thanks.



I think you can only go one step up in your inheritence
hierarchy

 
Ranch Hand
Posts: 2373
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I completely agree with Ragu. U cannot call the most top class's method directly rather go one step up in your inheritence heirarchy i.e. one-by-one.
See the slightly modified code below :

------------------
azaman
 
Cherry Sta. Romana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you. My friend was asking about this and I said that I don't think that it is possible. However, we both attended a training on Java and we both remember that our trainor said that it is possible using some form of super.super.. Anyway, we mis-understood it probably.
Thanks again.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF your instructor said that, I'd consider getting another instructor.
April

Originally posted by Cherry Sta. Romana:
Thanks to both of you. My friend was asking about this and I said that I don't think that it is possible. However, we both attended a training on Java and we both remember that our trainor said that it is possible using some form of super.super.. Anyway, we mis-understood it probably.
Thanks again.


 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps he/she was referring to the fact that super actually refers to the next feature up the inheritance hierarchy, regardless of whether that is the immediate superclass or not. In this way, c3 could invoke c1's "method" directly...
Example:
<code>
class C1 {
public void method() {
System.out.println("C1");
}
}
class C2 extends C1 {
//public void method() {
//super.method();
//System.out.println("C2");
//}
}
class C3 extends C2 {
public void method() {
super.method()
System.out.println("C3");
}
}
</code>
Since C2 did not override "method", the call to super.method in C3 would invoke the method from C1.
Maybe this is what your instructor was talking about...
Either way, super.super is definately not legal.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!