In Java, super keyword is used to access Superclass Member in subclass.You can use super keyword in following scenarios.
Syntax
In Subclass Constructor
super()
This calls No-arguments constructor present in super class.
super(parameters)
This will call any matching constructor with same parameters.
Call to super must be first statement in constructor. Mind It!!
Otherwise you will get compile time error.It is not allowed.As stated in Java Language Specification:
ConstructorBody: {
[ExplicitConstructorInvocation] [BlockStatements]
}
Other code statements should come after explicit constructor invocation. As constructors instantiates an object, they should get executed before any block of code.
Code Example
In example we have extended SuperBit
. ExtendedSuperBit
have an overrided constructor with additional parameter. But to initialize an Object we need to call SuperBit
constructor. We have accomplished this using super(code)
;
class SuperBit {
private int code;
SuperBit(int code) {
this.code = code;
}
public void encrypt(int key) {
code = code - key;
System.out.println("encrypt: subtraction");
}
public void decrypt(int key) {
code = code + key;
System.out.println("decrypt: addition");
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
class ExtendedSuperBit extends SuperBit {
private String author;
ExtendedSuperBit(int code, String author) {
super(code);
this.author = author;
}
public String getAuthor() {
return author;
}
@Override
public void encrypt(int key) {
super.encrypt(key);
setCode(getCode() * key);
System.out.println("encrypt: multiplication");
}
@Override
public void decrypt(int key) {
setCode(getCode() / key);
System.out.println("decrypt: divide");
super.decrypt(key);
}
}
/**
* Created by dinsaw on 1/4/2015.
*/
public class SuperDemo {
static final int KEY = 8;
public static void main(String[] args) {
ExtendedSuperBit bit = new ExtendedSuperBit(10, "Bitman");
System.out.println("Initialized bit = " + bit.getCode());
bit.encrypt(KEY);
System.out.println("Encrypted bit = " + bit.getCode());
bit.decrypt(KEY);
System.out.println("Decrypted bit = " + bit.getCode());
}
}
Output:
Initialized bit = 10
encrypt: subtraction
encrypt: multiplication
Encrypted bit = 16
decrypt: divide
decrypt: addition
Decrypted bit = 10
Try this Error
Comment super(code);
in above code example. You will get error.
java: constructor SuperBit in class SuperBit cannot be applied to given types; required: int found: no arguments reason: actual and formal argument lists differ in length
If you are using any IDE (Eclipse, IntelliJ) or compile the code then you will be able to see somewhat cause of this error. Most of the IDE will show error on constructor like this.
There is no default constructor available in SuperBit
Remember, when Subclass constructor do not invoke superclass constructor explicitly then Java inserts a call to SuperClass Default Constructor. If SuperClass does not have default constructor and have parameterized constructor then compile time error will occur.
To access SuperClass members
super.methodFromSuperClass(params-if-present)
by using super qualifier, you can call all accessible methods of super class. We have demonstrated this in code example. See super.encrypt(key);
and super.decrypt(key);