aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to use super Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how to use super" Watch "how to use super" New topic
Author

how to use super

Cherry Sta. Romana
Greenhorn

Joined: Aug 08, 2001
Posts: 18
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

Joined: Jul 20, 2001
Posts: 464
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

Joined: Jul 05, 2001
Posts: 2370

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


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Cherry Sta. Romana
Greenhorn

Joined: Aug 08, 2001
Posts: 18
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

Joined: May 02, 2001
Posts: 48
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

Joined: Oct 27, 2000
Posts: 61
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.
 
wood burning stoves
 
subject: how to use super