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 ?

9
  • You seem to have defined Homme twice and omitted Animal from your question. Please correct this Commented Oct 21, 2018 at 22:58
  • @Phil I added the missing class Commented Oct 21, 2018 at 23:00
  • 3
    You call super desc in everything, why wouldn't they print? Commented Oct 21, 2018 at 23:01
  • You have 2 levels of inheritance and are including super prints everywhere along with regular printing statements Commented Oct 21, 2018 at 23:02
  • 1
    See also How do I compare strings in Java? Commented Oct 21, 2018 at 23:20

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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 ?
@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
@TrevörAnneDenise as String are object, this is not a suprising behavior ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.