Sie sind auf Seite 1von 4

super Object Reference Lawrence M.

Brown
ktee@erols.com

super Object Reference


The super keyword treats a reference of the current object as a reference (instance) of the
superclass. When a method is invoked via super, the method implemented is based on the
superclass.

1. super used in a constructor


The keyword super can access the superclass constructor. The use of super is analogous to
the use of this to invoke another class constructor method from within an existing constructor.

• 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().

Example, super and this in Constructors (superclass TwoDshape not shown)

public class Circle extends TwoDshape


{
private static final double DEFAULT_RADIUS = 10.0;
private double radius;
• Java implicitly adds a call to super() as the first line.
// default constructor
public Circle()
{ radius = DEFAULT_RADIUS; • Preferred default constructor:
} public Circle()
{ this( 0.0, 0.0, DEFAULT_RADIUS );
}
//-------------------------------------------------------
// constructor passing in Circle radius
public Circle( double radius ) • Call to another constructor in the same class.
{ this( 0.0, 0.0, radius); • super not implicitly called.
}

//--------------------------------------------------------
// 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).
}
}

1998 1 December 31, 1998


super Object Reference Lawrence M. Brown
ktee@erols.com

2. super to access superclass hidden variables

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

Example, Access Hidden Variable Output

class A value = 1
{ public int value = 0; this.value = 1
} super.value = 2
//------------------------------------------
class B extends A
{

public int value = 0; // hides value in 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

System.out.println( "value = " + value );


System.out.println( "this.value = " + this.value );
System.out.println( "super.value = " + super.value );
}
}

1.2 super to access superclass overridden methods

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 )

1998 2 December 31, 1998


super Object Reference Lawrence M. Brown
ktee@erols.com

Example 1, super to access overridden methods

//------------------------------------------

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; }

// g overrides superclass A method g()


public double g() { 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() );
}
}

Output: g() = -10.0


f() = 10.0
super.f() = 100.0
super.g() = -1000.0

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.

1998 3 December 31, 1998


super Object Reference Lawrence M. Brown
ktee@erols.com

Example 2, super to access overridden methods

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() );

// attempt to access overridden method k() in Class W -- fails


System.out.println("Method super.k() in Class Y = " + super.k() );
}
}

//------------------------------------------

public class Overridding2


{ public static void main ( String[] args )
{
Y y = new Y();
y.printY();
}
}

Output : Method k() in Class Y = 99.99


Method super.k() in Class Y = 99.99

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.

1998 4 December 31, 1998

Das könnte Ihnen auch gefallen