Why is one method that uses polymorphism being called in place of another in case of inheritance?

Asked

Viewed 110 times

0

Could someone explain why the output of the program is:

Primate walking Mammal listening Man seeing.

Shouldn’t be?:

Primate walking Mammal listening Mammal selling

Classes

Year: 2019 Banking: IDECAN Organ: IF-PB Proof: IDECAN - 2019 - IF-PB - Professor - Informatics

  • 1

    The question is very confusing, saying things that don’t make sense, without making it clear what you’re talking about at each point making it ambiguous. Writing a better text will be a good exercise that will help understand and if you still have a relevant question it is easier to give an adequate answer.

  • @Maniero, I’d like to know why the output of the program is Primate walking Mammal listening to Man watching. What I could not understand was why the copilot used the listening() method of the Man class and not the Mamifero class.

  • 1

    At no point in the question do you speak of exit. What is coping? What does it have to do with that? If that’s what I’m thinking he has nothing to do with the problem, so he’s confused. Maybe you’re using incorrect terms and you’re learning it all wrong, so you may be having trouble asking the question. Edit the question giving as much detail as possible. Well in the problem before, this will help understand the problem. try using the rubber duck technique https://pt.wikipedia.org/wiki/Debug_com_Pato_de_Borracha, it helps a lot to enter your own problem.

  • Post the code as text instead of image. I reopened because now I can understand the question, but it would be better the text so we can answer better demonstrating.

  • The output you are saying is impossible, for you have no parent method to call this chain of methods. Apparently, you only have a structure of methods, and in this case there would be no possible way out.

  • The main calls everything, it is fact..., but he is tied in a method, father who is built inside himself, I do not know if it works like this... the main needs to exist before making any instance...

  • @Ivanferrer is true, he was so confused that I ate a ball, I can answer. But everything is ok: https://ideone.com/Buc4xM.

  • But that’s polymorphism, as far as I know, java does that.

Show 3 more comments

1 answer

4


That’s how polymorphism works. When you created an object of the type Homem he tries to execute the methods of this object, right? And he found the ver(). If a method does not exist in this type what does it do? It looks for the method in the ascending type of it, in case no Primata, and finds the andar(), right? And if you still can’t find search in the next ascendant, so in the Mamifero finds the ouvir(), Right? If you’re still looking for some other Object since all classes have as their final ascendant the class Object, and finally if the method is not found anywhere gives compilation error. So far so good?

So when you call the m.andar() is calling the method of Primata. And you acknowledge that it is right to print its contents. In andar()is calling for ouvir(), then it will search for the method in the most specific class possible again. In case only the Mamifero has this method, and will execute it. Which in turn calls the method ver(). What method will he call? Again the most specific class method possible which in this case is the Homem. You will always search in the most specific class possible and then try in the others by climbing one level at a time. I think this is where you’re thinking wrong.

For some reason he thinks he should use the closest method possible. For some reason you are thinking of classes as composition and not as inheritance, which is a common mistake. When you have inheritance you should forget the other classes, you should only think of the more specific class as being complete. And it’s complete if you take the methods that don’t exist in it from the other classes. The method is not written there, but the method exists in it, it is only borrowing from another class, but the object is the same.

It’s not like that, but we can think of the class this way:

class Homem {
    protected void ouvir() {
        System.out.print("Mamifero ouvindo ");
        ver();
    }
    protected void andar() {
        System.out.print("Primata andando ");
        ouvir();
    }
    protected void ver() {
        System.out.print("Homem vendo ");
    }
}
class Main {
    public static void main(String[] args) {
        Homem m = new Homem();
        m.andar();
    }
}

I put in the Github for future reference.

Note that it was composed by the method that already existed in it, and the other methods of the other classes, giving preference to the more specific class always. In fact it has language that makes inheritance (if you can call it that) exactly in this way creating a complete class, it’s just not that common. What languages like Java do is maintain a virtual method call table where it has the addresses of the methods, and the address that is placed there is always as specific as possible.

Classes are like models, you’re probably thinking of them as objects and that’s why it’s confusing. See What is the difference between a class and an object?.

Who is in charge is the concrete object that was created, it is he who will determine who will be called, not what is in the other types, not what is in the declared type, so who is in charge in these cases is Homem and not Mamifero.

Without polymorphism it would all be called the Mamifero, but then neither the Primata would be in play. Without the polymorphism who would be the declared type, in case Mamifero and he has all the necessary methods. Even if he had not all he would have nothing he could do not only because he has no polymorphism, but also because he cannot be aware of classes created from it, then the class would be abst4rata and could not be instantiated.

Anyway this example shows well how the mechanism works, but it usually misindicates how if truly object-oriented program, it is a shame that use both this type of example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.