And again I step on a rake, moving from theory to practice.

There is an abstract class Animal . This class has a makeNoise(), method that displays the message "I am an animal!". There are also classes Cat and Dog , which are inherited from the super-class Animal . In each of them, I overridden the method makeNoise() . Now, depending on whether the cat was created or the dog, the method displays "Meow" or "Woof."

But! Let's say it became necessary for an object of type Cat call the method makeNoise() , but not its own method, which gives us "Meow", but the method of the super-class, which tells us "I am an animal". How to implement it? My attempts below, the result is 0, the overridden method is called :(

 public class Main { public static void main(String[] args) { /*Объекты типа animal*/ Animal cat = new Cat("Маруся"); Animal dog = new Dog("Шарик"); /*Объекты своих собственных типов*/ Cat cat2 = new Cat("Маруся 2"); Dog dog2 = new Dog("Шарик 2"); System.out.println("-------------------"); cat.makeNoise(); cat2.makeNoise(); } } public abstract class Animal { private String name; public void setName(String name) { this.name = name; } public String getName() { name = this.name; return name; } public void makeNoise() { System.out.println("Я животное!!11!!"); } } public class Cat extends Animal { public Cat(String name) { this.setName(name); System.out.println("Новая кошка создана. Ее имя: " + getName()); } @Override public void makeNoise() { System.out.println(getName() + " Сказала: " + "Мяу мяу"); } } public class Dog extends Animal { public Dog(String name) { this.setName(name); System.out.println("Новая собака создана. Ее имя: " + getName()); } @Override public void makeNoise() { System.out.println(getName() + " Сказала: " + "Гав гав"); } } 

PS Tell me, please, is there a polymorphism in the example I described?

    3 answers 3

    This is done like this:

     super.makeNoise(); 
    • They explained below that I misunderstood something, but I still want to apply your method, see what happens .. How can I apply it in main? I tried super.cat.makeNoise (); and differently .. no way .. Or is it necessary to describe in animal classes? But if this is so, then it turns out that from main I cannot call cat's (Animal) method on cat? Head around OOP: D - Ivan Blohin
    • 2
      super can only be used to refer to your parent class, to someone else's. So you need to write this in the class of the corresponding animal. And "outside" you can call only one version of the method - the latest in the sense of overriding, this is called virtual methods (see c ++). - Roman

    It can be seen that your question appeared after your previous question .

    Apparently, you do not quite understand at the moment why polymorphism is needed. The point is that you have the same method signature for different types (the same method names with the same return type and the same transmitted types), but the code in these methods is different.

    To do this, you need inheritance, you get not only access to the parent's code and access interface (the same signature), although it is better to use interfaces when designing.

    I recall that Polymorphism refers to the ability to work with several types as if it is the same type and at the same time the behavior of each type will be unique depending on its implementation.

    You do the opposite, have heirs, but try to call the code of the parent.

    • one
      Thank you, but I understood polymorphism in my time as an opportunity to call the code of parental classes .. Thanks again :) - Ivan Blohin
     public void makeNoise() { this.super().makeNoise(); } 

    I don’t remember it, but something with super is for sure