1

I built a program in order to test Java inheritance, there are several classes:

public class Animal {
    String name = "";
    String kind = "";

    public Animal() {
        System.out.println(desc());
    }

    public Animal(String name) {
        this.name = name;
        System.out.println(desc());
    }


    String desc() {
        if (name == "") {
            return "Je suis un animal.";
        } else {
            return "Je suis un animal de nom " + name + ".";
        }
    }
}




public class Mamifere extends Animal {    
    public Mamifere() {
        System.out.println(desc());
    }

    public Mamifere(String name) {
        this.name = name;
        System.out.println(desc());
    }

    String desc() {
        return super.desc() + " Je suis un mamifère.";
    }    
}

public class Homme extends Mamifere {    
    public Homme() {
        System.out.println(desc());
    }

    public Homme(String name) {
        this.name = name;
        System.out.println(desc());
    }

    String desc() {
        return super.desc() + " Je suis un homme.";
    }    
}

public class Chien extends Mamifere {    
    public Chien() {
        System.out.println(desc());
    }

    public Chien(String name) {
        this.name = name;
        System.out.println(desc());
    }

    String desc() {
        return super.desc() + " Je suis un chien.";
    }    
}

When I run this code:

public class TestAnimal {

    public static void main(String[] args) {
        Animal[] animaux = new Animal[5];
        animaux[0] = new Animal("Truc");
        animaux[1] = new Animal();
        animaux[2] = new Chien("Medor");
        animaux[3] = new Homme();
        animaux[4] = new Homme("Robert");

    }

}

I get this output:

Je suis un animal de nom Truc.
Je suis un animal.
Je suis un animal. Je suis un mamifère. Je suis un chien.
Je suis un animal. Je suis un mamifère. Je suis un chien.
Je suis un animal de nom Medor. Je suis un mamifère. Je suis un chien.
Je suis un animal. Je suis un mamifère. Je suis un homme.
Je suis un animal. Je suis un mamifère. Je suis un homme.
Je suis un animal. Je suis un mamifère. Je suis un homme.
Je suis un animal. Je suis un mamifère. Je suis un homme.
Je suis un animal. Je suis un mamifère. Je suis un homme.
Je suis un animal de nom Robert. Je suis un mamifère. Je suis un homme.

Why is there so many repetitions for the objects constructed from the argument less constructor ?

halfer
  • 19,824
  • 17
  • 99
  • 186
Pop Flamingo
  • 3,023
  • 2
  • 26
  • 64

1 Answers1

1

As Chien extends Mammifere and Mamifere extends Animal : when you want to instanciate an Chien it also builds an Mamifere as it's a super-type and so it also builds an Animal as it's also a super type

  • you're seeing the desc method launched by Chien constructor
  • you're seeing the desc method launched by Mamifere constructor
  • you're seeing the desc method launched by Animal constructor

The same happens hor Homme

azro
  • 53,056
  • 7
  • 34
  • 70
  • That's interesting. Why does some IDEs (such as Eclipse), generate constructors where an explicit call (`super(args)`) to the superclass constructor is done ? Is there any difference with not calling it ? – Pop Flamingo Oct 21 '18 at 23:09
  • 1
    @TrevörAnneDenise when you need to call super-constructor with parameter it's required to use super(arg1, arg2, ..) but for no-param it's good practice bu it' would work without – azro Oct 21 '18 at 23:22
  • @TrevörAnneDenise as String are object, this is not a suprising behavior ;) – azro Oct 21 '18 at 23:32