How to correctly implement the polymorphism?

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

    
asked by anonymous 01.06.2018 / 05:11

2 answers

6

Wrong question

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

The most important thing is not to do polymorphism where you do not need it. Actually do not do object orientation where you do not need this guidance (which can be called a 'paradigm', though there are doubts as to whether it is ). People tend to cling to tools and want to use them anyway. It's kind of like buying a culinary torch and wanting to use it for things that are not brolè cream (okay, it works for some other things, but few, not for burning out anything).

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

What is polymorphism

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

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

Polymorphism can be solved at compile-time or execution 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 not tell how to implement it. Unless you are willing to implement the mechanism. But almost no one wants, usually this is only necessary if you are creating a language. You have a question that shows this a bit .

Even though you're talking about using the mechanism depends on language , you can not respond generically.

Another much-used mechanism , but can be implemented of several works . And it has a form that is a polymorphism specialization, but we give it another name . It is possible to exist others, but these are the most 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 answered this. If you still have specific questions, you can open new questions about what you have missed. I think the better answer would be mine .

There is a question with several links for other answers about the subject .

And finally a usage example that I will not post here because this has already been replied to. Other examples . I put a classic example, although not exactly how it is used in practice, gives a good understanding. And last .

    
01.06.2018 / 05:38
3

I'll stick to just one polymorphism example:

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");
    }
}

So we have a method to hear the sound of an animal. Note that this is a generic method. He will receive an animal and need to call the emitirSom() method of that animal, whatever. 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 ouvirSom() method:

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

Prints:

//Au au
//Miau

Again, realize that the ouvirSom(Animal animal) method did not know which animal would be passed to it at compile time, it could be anyone who extended Animal. At the time of execution, when the parameter was passed, the polymorphism would be in charge of calling emitirSom() of the animal used, and that is what happened, correctly printing the sound of the animal.     

01.06.2018 / 13:26