Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to use super

 
Cherry Sta. Romana
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.

 
Ragu Sivaraman
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

 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • 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.
 
April.Johnson
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.

 
Guy Reynolds
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic