Hello. I study OOP with Java and came across such a rake: I have a class Animal. It has only one name variable. When creating an Animal object, a constructor is called, which displays the message "A new animal is created. Its name is name.". Next, I created a Cat class that inherits from Animal and I want it when I do this Animal cat = new Cat("Кошка");
I got the message “A new cat was created. Its name is name”. I'm trying to do it in the Cat class constructor, but he swears, he says, you can only
public Cat(String name) { super(name); }
But I do not want that. Is it possible to override the constructor in the child class? Or is it nonsense and need to act differently? Full code below.
public class Main { public static void main(String[] args) { Animal animal = new Animal("Лео"); Animal cat = new Cat("Кошка"); } } public class Animal { private String name; public Animal(String name) { this.name = name; System.out.println("Новое животное создано. Его имя: " + this.name); } } public class Cat extends Animal { public Cat(String name) { super(name); } }
Тогда я не смогу давать имена животным при создании
what is this statement based on? - Alexey Shimansky