I looked at a book question and the output for the below code is "MammalPlatypus". Can you please explain why? I see that if a Platypus object is created (no arg), the Platypus constructor calls the Mammal constructor then back to itself, resulting in MammalPlatypus. But I see it is creating a Mammal object below, so why not only the Mammal constructor is invoked? Can't you create a superclass instance from a subclass? why would that end up invoking the subclass constructor?
What is the output of the following code?
1: class Mammal {
2: public Mammal(int age) {
3: System.out.print("Mammal");
4: }
5: }
6: public class Platypus extends Mammal {
7: public Platypus() {
8: super(8);
9: System.out.print("Platypus");
10: }
11: public static void main(String[] args) {
12: new Mammal(5);
14: }
14: }
What is the output of the following code?
1: class Mammal {
2: public Mammal(int age) {
3: System.out.print("Mammal");
4: }
5: }
6: public class Platypus extends Mammal {
7: public Platypus() {
8: super(8);
9: System.out.print("Platypus");
10: }
11: public static void main(String[] args) {
12: new Mammal(5);
14: }
14: }