Beruflich Dokumente
Kultur Dokumente
Brown
ktee@erols.com
• If super is used in a constructor, then super must appear as the first statement within the
constructor.
• If a call to the superclass constructor is not explicitly stated using the keyword super( parameters ),
then Java implicitly inserts a call to the superclass's no-argument constructor via super().
However, one exception exists. If the first line in the constructor is this( parameters ), a call to
another constructor in the same class, then execution continues to the called constructor in the
class without calling super().
//--------------------------------------------------------
// constructor passing in center (x,y) and radius
public Circle( double x, double y, double radius )
{ super( x, y ) ; • Superclass constructor explicitly called
this.radius = (radius > 0.0) ? radius : DEFAULT_RADIUS; (with parameter passing).
}
}
A variable redefined in the subclass with the same name as a variable in the superclass hides
the variable in the superclass. To access any non-private hidden variable in the superclass proceed
the variable name with the keyword super, as shown below:
super.hidden-variable-name
class A value = 1
{ public int value = 0; this.value = 1
} super.value = 2
//------------------------------------------
class B extends A
{
public B()
{ value = 1; // variable value in class B
this.value = 1; // variable value in class B
super.value = 2; // variable value in class A
Similar to accessing hidden variables in the superclass, the keyword super can be used to
access overridden methods in the superclass. To access an overridden method use
super.overridden-method-name( parameter-list )
• super can only be used to invoke overridden methods from within the class that does the
overriding.
• Successive use of super will not access overridden methods higher up in the hierarchy. The
following is illegal syntax
super.super.overridden-method-name( parameter-list )
//------------------------------------------
class A
{
public double f() { return 1000.0; }
public double g() { return -1000.0; }
}
//------------------------------------------
class B extends A
{
// f overrides superclass A method f()
public double f() { return 100.0; }
}
//------------------------------------------
class C extends B
{
// f overrides superclass B method f()
public double f() { return 10.0; }
// constructor
public C()
{ System.out.println("g() = " + g() );
System.out.println("f() = " + f() );
System.out.println("super.f() = " + super.f() );
System.out.println("super.g() = " + super.g() );
}
}
The above example illustrates the use of the keyword super to access an overridden method.
As shown in the diagram, Class B can use the keyword super to access the overridden method f() in
Class A. In Class C, the keyword super can be used to access both overridden methods f() and g() in
Class B. The keyword super will expose only the most recent overridden method. If the method has
been overridden multiple times in the tree hierarchy, as in the case of method f(), earlier versions of
the method farther up the tree cannot be accessed through multiple uses of super. The use of
super.super.f(), for example, is illegal syntax.
class W
{
public double k() { return 9999.99; }
}
//------------------------------------------
class X extends W
{
// k overrides superclass W method f()
public double k() { return 99.99; }
}
//------------------------------------------
class Y extends X
{
public void printY()
{ System.out.println("Method k() in Class Y = " + k() );
//------------------------------------------
Example 2 demonstrates that use of the keyword super from a class that does not override the
method simply accesses the method in the superclass (which is the same method through
inheritance). The use of super from a class that doesn't override the method doesn't access any
previously overridden method higher up the hierarchy.
In class Y, the method k() is inherited from class X. The use of super.k() in class Y simply
accesses k() in the superclass (class X), and does not access any earlier overridden version of k(),
for example, the overridden method of class W. However, in class X the use of super.k() does
indeed access the overridden method in the superclass W. An overridden method can be accessed
from the class that does the overriding. In summary, the use of super in a class will only access the
previous overridden version of the method if the class itself overrides the method.