This question has already been answered:

There is an interface ITransport , with a description of the void Show(); method void Show();

This interface implements an abstract class.

abstract class Naz_Transport:ITransport and, accordingly, the method public void Show(){} .

There is a class that inherits from the above class.

class Avtobus:Naz_Transport and it also has a method

 new public void Show() { base.Show(); + еще некоторые данные, принадлежащие этому классу } 

The problem is the following, in Maine I create an array of objects

 ITransport[] arr = { new Avtobus(тут данные для конструктора) }; 

And when I try to call the arr[0].Show(); for some reason, the method from the Naz_Transport class Naz_Transport , although I need the method from the Avtobus class to Avtobus .

What is wrong doing? Thank you in advance.

Reported as a duplicate by Grundy members, aleksandr barakin , Community Spirit 20 Mar '16 at 18:21 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • nothing is clear. but you yourself call the base method base.Show(); - Grundy
  • That's right, but in addition to it, I want to display another line (data from the class Avtobus), they are not displayed. But if I simply create an Avtobus class object in Maine, then everything is fine, but when using arrays of interface objects, it does not work. - Pyrejkee
  • 2
    aaaa, now it's clear, the problem is in new public - use override and everything will work - Grundy
  • It really worked, and why it was impossible to do it the way I did? - Pyrejkee
  • 2

1 answer 1

The problem is in the new public - use override and it will work.


As written in the MSDN manual: Using the "Override" and "New" keywords

In C #, a method in a derived class can have the same name as a method in a base class. You can set the way that methods interact with the new and override keywords. The override modifier extends the base class method, and the new modifier hides it.

The main difference between these keywords is noticeable when the object is brought to the base class.

If we consider an example from the question:

  1. Base class implements interface
  2. Heir overrides base class method
  3. Objects are stored in an array of interfaces.

Since the interface in this case implements only the base class, placing an object in an array is equivalent to casting it to the base class.

Thus, the rules are similar to the case.

 Base a = new Derived(); a.Show(); 

And in the case of using new , the function of the base class will be called.

It is worth noting that if the interface implements a class and a successor, then such a problem would not arise, since when casting to the interface there would be no reduction to the base class.

Example with different ad variations

 public static class Program { interface IA { void Show(); } class A : IA { public virtual void Show() { Console.WriteLine("A"); } } class B : A { new public void Show() { Console.WriteLine("B"); } } class C : A { override public void Show() { Console.WriteLine("C"); } } class D : A,IA { new public void Show() { Console.WriteLine("D"); } } public static void Main() { IA[] ia = new IA[] { new B(), new C(), new D() }; foreach (var i in ia) { i.Show(); } } /* Выведет: A C D */ }