How to implement polymorphism correctly?

Asked

Viewed 144 times

3

One of the first things we hear about when we study the object-orientation paradigm is polymorphism but how can we or "should" implement it, since it’s a concept we see various forms of implementation, how can I better understand how to implement and when to use it?

  • I suggest checking the good cases of using polymorphism in the real world, for example the hierarchies of Views in UI frameworks. Object orientation (and polymorphism along with it) are highly hyped, winning frameworks such as iOS (Cocoa Touch) are notoriously "horizontal", meaning they have no deep class hierarchies, nor do they use polymorphism for everything, so it’s nice to also look for situations where it doesn’t fit.

2 answers

7


Wrong question

You’re thinking about the wrong question. It’s right to think about a problem and look for the right tools to solve it in the best way. One of these tools is polymorphism. And there are different mechanisms to meet this demand, each language implements in one way (or more than one).

The most important thing is not to do polymorphism where you don’t need it. In fact do not do object orientation where you do not need this orientation (which may be called paradigm, albeit there is doubt if it is). People tend to get attached to tools and want to use them anyway. It’s kind of like buying a cooking torch and wanting to use it for things that aren’t cream brolè (Okay, it’s good for some other things, but few, not to go out burning anything).

So understand the polymorphism and apply it when you need it.

What is polymorphism

A definition of polymorphism is the ability of something to be executed according to the state (or its type) at a given time. Use it when you need it. So it’s the ability to take various forms.

Another way to define him is to be one replacement of a conditional (usually if or switch) to decide which behavior or state to use at a given time. This is one of the reasons that is said to be best program for interfaces.

Polymorphism can be solved at compile or run time, depending on the mechanism used and the need.

Mechanism

In general you implement a problem and use polymorphism as a mechanism, so you can’t tell how to implement it. Unless you want to implement the mechanism. But almost nobody wants to, usually it is only necessary that you are creating a language. Has a question that shows a little this.

Even if you’re talking about using the mechanism depends on the language, cannot answer generically.

Another widely used mechanism, but it may be implemented in several ways. And it has a shape that’s a specialization of polymorphism, but we give another name. There may be others, but these are the most commonly used and documented, so the 3 forms presented are the most correct for the general cases.

Where to learn and see examples

We can show what it is to understand and then apply when necessary. I and other people have already responded about it. If you still have specific questions you can open new questions about what was missing. I think the best answer would be this mine.

There’s a question with several links for other answers on the subject.

And finally a example of use that I won’t put here because that has already been answered. Other examples. I put a classic example, although this is not exactly how it is used in practice, it gives a good understanding. And lastly.

4

I’ll stick to just one example of polymorphism:

Every animal emits a sound, so any animal that considers itself an animal has to implement this method emitirSom():

interface Animal {
    void emitirSom();
}

The dog is an animal, so he has his method:

class Cachorro implements Animal {

    @Override
    public void emitirSom() {
        System.out.println("Au au");
    }
}

And the cat too:

class Gato implements Animal {

    @Override
    public void emitirSom() {
        System.out.println("Miau");
    }
}

We then have a method to hear the sound of an animal. Note that it is a generic method. It will receive an animal and need to call the method emitirSom() of that animal, whatever it is. This is where polymorphism occurs. At compile time, all he knows is that some animal will appear there as a parameter.

static void ouvirSom(Animal animal) {
        animal.emitirSom();
}

Finally, let’s test the method ouvirSom():

public static void main(String[] args) {
    ouvirSom(new Cachorro());
    ouvitSom(new Gato());
}

Prints:

//Au au
//Miau

Again, note that the method ouvirSom(Animal animal) did not know which animal would be passed to him at compile time, it could be anyone who extended Animal. At runtime, when the parameter was passed, the polymorphism would call the emitirSom() of the animal used, and that’s what happened, correctly printing the sound of the animal.

  • I imagine he doesn’t just want an example, because if it is, it has been answered before, as my answer shows.

  • @Maniero Your answer is quite elucidative, excellent, however, the example you linked, in my opinion, is confusing for those who, as the OP shows, seems to be trying to begin to understand polymorphism. In fact, I tried with my example to supplement your excellent response and leave as reference for future queries of other people.

  • Okay, it may be, although it’s a real example of use, its artificial and in practice it’s not used, but okay, but that’s not the question I was talking about, is that I don’t think this answers what was asked, and if it does, the question is duplicated. Even because it has various ways of using polymorphism, and this cannot be considered the correct form, just an option. If he wants to understand the basics, the question is duplicate. I’ll put a few more links

Browser other questions tagged

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