Forcing the subclass [closed]

2

How do I 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");
    }
}
    
asked by anonymous 20.04.2015 / 01:08

2 answers

1

Adding the modifier virtual in the base class method and then doing the override in 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");
    }
}

In this way, indicated that the derived classes that do not override of the method, print "class transport ...".

If you type override (as in the case of class Aviao), when invoking the exibeDados() method, it will execute whatever is in the derived class method (in this case print "Aviao").

    
20.04.2015 / 01:12
1

To force the subclass to implement the ExibeDados method, the method must be abstract (marked with the keyword abstract ). Consequently, the Transporte class must also be abstract and therefore can never be instantiated directly (which seems to me to be the desired one 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 right in this situation - the example of VS Airplane / Ship Transport, and the example of the 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 can not be abstract, then the answer is: it is not possible. A non-abstract class can not force derived classes.

Note: The convention dictates that the method names are written in PascalCase , then the method should be called ExibeDados instead of exibeDados .

    
20.04.2015 / 09:32