Reputation: 1
While you cannot make an instance of an abstract class, in this example the super keyword is used to reference the instance variable of the super class, hence a reference that is provided in the abstract class.
public abstract class Abstract {
protected int n1;
protected int n2;
public Abstract(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
}
public class Instance extends Abstract {
public Instance(int n1, int n2) {
super(n1, n2);
}
public int makeSum() {
return super.n1 + super.n2;
}
public static void main(String[] args) {
Instance myInstance = new Instance(1, 2);
System.out.println(myInstance.makeSum());
}
}
Is there an instance of the abstract class created "in the background" - or how do I have to read this?
Upvotes: -1
Views: 61
Reputation: 25812
In your example there is only one instance. It contains members defined in its class and its parent class. super is used to reference members defined in the parent.
Upvotes: 0