Polymorphism or inheritance?

Asked

Viewed 1,490 times

10

I’m learning about polymorphism and I’ve been wondering if what I’m doing is actually polymorphism or just inheritance?

If it’s polymorphism what’s in my code that I can clearly identify is polymorphism?

If it is not, what could be done to be polymorphism?

public abstract class Pessoa
{
    public abstract int Id { get; set; }
    public abstract string Nome { get; set; }

    public abstract void Andar();
}

public class Comprador : Pessoa
{
    public override int Id { get; set; }  
    public override string Nome { get; set; }      

    public override void Andar()
    {
        throw new NotImplementedException();
    }

    public void Comprar()
    {

    }
}

public class Vendedor : Pessoa
{
    public override int Id { get; set; }
    public override string Nome { get; set; }

    public override void Andar()
    {
        throw new NotImplementedException();
    }

    public void Vender()
    {

    }
}

public class Sistema
{
    public void MeuMetodoTeste()
    {
        Comprador pessoaComprador = new Comprador();
        pessoaComprador.Comprar();

        Vendedor pessoaVendedor = new Vendedor();
        pessoaVendedor.vender();
    }
}

1 answer

10


You’re doing both. When you put Comprador : Pessoa or Vendedor : Pessoa is making inheritance, ie the first name is defining a class that will be composed initially by the class referenced in the second name. Is Comprador and Vendedor will be subclasses and subtypes of Pessoa.

These two concrete classes can be used in the place where the abstract class is expected Pessoas, so in this case polymorphism will occur. In this case it will actually occur elsewhere in the code. It did not occur in your example because there did not need to be a replacement. These classes were used to create objects of the declared type itself and were not used elsewhere.

You can see that the polymorphism will occur more clearly in the methods declared in Pessoa. Since they are all abstract, they must be implemented in the descending classes. And for these implementations to be used a polymorphic mechanism needs to be adopted.

Here begins to occur polymorphism concretely:

public class Sistema {
    public void MeuMetodoTeste() {
        var pessoaComprador = new Comprador();
        pessoaComprador.Comprar();
        UmOutroMetodo(pessoaComprador);
        var pessoaVendedor = new Vendedor();
        pessoaVendedor.vender();
        UmOutroMetodo(pessoaVendedor);
    }
    //note que se passar um objeto do tipo Pessoa nem funcionaria de fato, deve ser conreto
    public void UmOutroMetodo(Pessoa pessoa) {
        Console.WriteLine(pessoa.Nome); //vai pegar o que foi usado na classe concreta
        pessoa.Andar(); //vai lançar a exceção
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Note that polymorphism occurs concretely when there is a virtual method (abstract methods are virtual by definition). A virtual method can be overwritten (abstracts must, after all, have no implementation). Note that there can only be substitution if the method has exactly the same signature. Substitution takes place through the keyword override.

This can be better explained in a more general sense in other questions (I have answered this in different ways in different locations - have better examples to show polymorphism in these questions):

  • I was confused you said that to give concretely that it is polymorphism when the method (should have) has the same signature, in the reply of @Gypsy Morrison Mendez he says it may have different signatures, I stood on the wall :/

  • 1

    @Mauricioferraz Note that Gabriel Katakura comments there that this is not polymorphism (at least not the classic you are asking in your question). That is an overload. I gave you several references (most are mine, but have other people) all very well voted that confirm what I said and have links for other references you can follow and see that I wrote the correct one. And I have gold in OOP http://answall.com/help/badges/187/orienta%C3%A7%C3%a3o-a-objetos :P :D I put more references in my answer. If I haven’t convinced you tell me, I’ll try to find another way.

  • I get it, I’m sorry for asking this question, I just stood on the fence because you guys are so well-regarded here on the forum and I have so little knowledge about it. Well then I will consider out of context the response of @Gypsy Morrison Mendez not to confuse my head anymore. : ) I’ll give you a complete pass on the references you posted. Thank you.

  • @Mauricioferraz has no reason to apologize, it’s in his role. If you still have other questions open new specific questions. The questions are as important to the success of the site as the answers. Yours will help many people who still do not understand the subject. If you want to know methods overload, have questions on the subject, if nothing suits you, ask. Be careful not to confuse overload of methods with operators. They are different things and I’ve seen some people responding wrong thing. Overload is not a concept of OOP (although polymorphism is also not exclusively).

Browser other questions tagged

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