Just do not understand how keywords this and super works in these cases. Here are 2 inheritance examples. Main method is common.
public static void main(String[] args) {
SeaCreature dolphin = new Dolphin();
dolphin.swim(); // Dolphin is swimming - in both cases
}
SeaCreature class is common.
class SeaCreature {
public void swim() {
System.out.println(this.getClass().getSimpleName() + " is swimming");
}
}
Dolphin class for the first case. In this case I would expect that this in swim() method gives SeaCreature object not Dolphin.
class Dolphin extends SeaCreature {
}
Dolphin class for second case. In this case I would expect that super in swim() method gives SeaCreature object not Dolphin.
class Dolphin extends SeaCreature {
@Override
public void swim() {
System.out.println(super.getClass().getSimpleName() + " is swimming");
}
}
However in both cases the result is "Dolphin is swimming". How this works?