Polymorphism or inheritance?

9

I'm learning about polymorphism and I'm in the doubt whether what I'm doing is actually polymorphism or just inheritance?

If it is polymorphism what do you have in my code that I can clearly identify that 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();
    }
}
    
asked by anonymous 22.09.2016 / 22:17

1 answer

10

You're doing both. When you put Comprador : Pessoa or Vendedor : Pessoa is doing inheritance, that is, the first name is defining a class that will be composed initially by the class referenced in the second name. It 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 Pessoas is expected, so in this case the polymorphism will occur. In case it will actually occur elsewhere in the code. It did not occur in your example because there was no need for a replacement. These classes were used to create objects of the declared type itself and were not used elsewhere.

Note that the polymorphism will occur more clearly in the methods declared in% with%. Since they are all abstract, they must necessarily 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 in concrete form:

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
    }
}

See running on dotNetFiddle .

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 substitution can only take place if the method has exactly the same signature. The substitution happens through the keyword Pessoa .

This can be better explained in a more general sense in other questions (I have already responded in different ways in different places - there are better examples to show the polymorphism in these questions):

22.09.2016 / 22:35