Force the subclass

Asked

Viewed 54 times

2

as a tool to force the subclass to display the correct data?

class Program
{
    static void Main(string[] args)
    {
        var tran = new List<Transporte>();
        tran.Add(new Navio());   // Objetos das subclasses   
        tran.Add(new Aviao());   // podem pertencer   
        tran.Add(new Onibus());  // ao tipo da Superclasse.   
        Console.WriteLine(" exemplo aplicação polimorfismo .");
        for (int i = 0; i < tran.Count; i++)
        {
            tran[i].exibeDados();
        }
        Console.ReadKey();
    }
}

public class Transporte
{
    public void exibeDados()
    {
        Console.WriteLine("classe transporte: Método para exibição dos dados.");
    }
}

public class Aviao : Transporte
{
    // Sobreposição do método da superclasse
    public void exibeDados()
    {
        Console.WriteLine("Aviao");
    }
}

public class Navio : Transporte
{
    // Sobreposição do método da superclasse
    public void exibeDados()
    {
        Console.WriteLine("Navio");
    }
}

public class Onibus : Transporte
{
    public void exibeDados()
    {
        // Sobreposição do método da superclasse
        Console.WriteLine("Onibus");
    }
}
  • 2

    Welcome to OS PT. Add more details about your problem as it is not clear what problem you are encountering and what the expected result is.

2 answers

1

Adding the modifier virtual in the base class method and then doing the override in the derived class:

public class Transporte
{
    public virtual void exibeDados()
    {
        Console.WriteLine("classe transporte: Método para exibição dos dados.");
    }
}

public class Aviao : Transporte
{
    // Sobreposição do método da superclasse
    public override void exibeDados()
    {
        Console.WriteLine("Aviao");
    }
}

Thus, it is indicated that derivative classes that do not override of the method, print "transport class...".

Case knife override (as is the case for the Aviao class), when invoking the method exibeDados(), will execute what is in the derived class method (in this case print "Aviao").

  • Despite the brevity of the issue, I think the key word here is force. Make the method virtual nay force the subclasses to re-define the method .

  • @dcastro but OP also does not indicate that it wants to force the classes to reset the method, it just says that it wants to force the derived classes to display the message. To be honest, it indicates that the problem is in the question of the PO, given that in my view both answers (and why I voted +1 in yours) are correct.

  • I agree, the question is open to interpretation and should be more specific.

1

To force the subclass implementing the method ExibeDados, the method must be abstract (marked with keyword abstract). Consequently, the class Transporte should also be abstract and as such can never be instantiated directly (which seems to me to be the desired, in this situation).

public abstract class Transporte
{
    public abstract void ExibeDados();
}

public class Aviao : Transporte
{
    public override void ExibeDados()
    {
        Console.WriteLine("Aviao");
    }
}

It seems to me that making the abstract class is the most indicated in this situation - the example of Transport VS Plane/Ship, and the example of Geometric Shape VS Square/Triangle are widely used to demonstrate situations where the base class should be abstract.

If the actual case is different from the question, and if the class cannot be abstract, then the answer is: it is not possible. A nonabstract class cannot force derived classes.

Note: the convention dictates that the name of the methods be written in Pascalcase, then the method should be called ExibeDados instead of exibeDados.

Browser other questions tagged

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