411 REFERENCE · Wikibooks
Java Programming/Keywords/super
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
is a keyword.
It is used inside a sub-class method definition to call a method defined in the super class. Private methods of the super-class cannot be called. Only public and protected methods can be called by the keyword.
It is also used by class constructors to invoke constructors of its parent class.
Super keyword are not used in static Method.
Syntax:
.<method-name>([zero or more arguments]);
or:
([zero or more arguments]);
For example:
Code listing 1: SuperClass.java
public class SuperClass {
public void printHello() {
System.out.println("Hello from SuperClass");
return;
}
}
Code listing 2: SubClass.java
public class SubClass extends SuperClass {
public void printHello() {
super.printHello();
System.out.println("Hello from SubClass");
return;
}
public static main(String[] args) {
SubClass obj = new SubClass();
obj.printHello();
}
}
Running the above program:
Command for Code listing 2
$Java SubClass
Output of Code listing 2
Hello from SuperClass
Hello from SubClass
In Java 1.5 and later, the "super" keyword is also used to specify a lower bound on a wildcard type parameter in Generics.
Code section 1: A lower bound on a wildcard type parameter.
public void sort(Comparator<? super T> comp) {
...
}
See also:
pt:Java/Palavra-chave/super
deep