I am dealing with some past exam papers and here I am not sure of the output. I think I am not clear about extends and super.
public class Superclass{
public boolean aVariable;
public void aMethod(){
aVariable = true;
}
}
class Subclass extends Superclass {
public boolean aVariable;
public void aMethod() {
aVariable = false;
super.aMethod();
System.out.println(aVariable);
System.out.println(super.aVariable);
}
}
I think that the second output would be true since it would refer to the super class and it is an object. However, I am not sure of the first output. Would it be just a value and print false or it is also an object?
The output will be:
false
true
Because in your Subclass
aVariable
is false
by default (so assignation aVariable = false;
is needless). Read more about Primitive Data Types default values.
And in Superclass
you initialize aVariable
as true
by invoking the superclass' method using the keyword super: super.aMethod();
. Read more about Accessing Superclass Members.
Take a look on demo.