C#: my derived class cannot override base class's interface method implementation, why?

vik santata

I've got the code below, I use class "B" to inherit class "A" while I wish to implement F function from interface IMy. But compiler tells my I'm hiding interface method "F". So the running result is "A".

I expect this program to output "B". I don't wish to use implicit interface implementation as I wish to use normal polymorphism in main function.

How to correct my code? Thanks.

public interface IMy
{
    void F();
}

public class A : IMy
{
    public void F()
    {
        Console.WriteLine("A");
    }
}

public class B : A
{
    public void F()
    {
        Console.WriteLine("B");
    }
}
class Program
{
    static void Main(string[] args)
    {
        IMy my = new B();
        my.F();
    }
}
Jakub Lortz

To override a method in C#, the method in base class needs to be explicitly marked as virtual. It doesn't matter if the method implements an interface method or not.

public class A : IMy
{
    public virtual void F()
    {
        Console.WriteLine("A");
    }
}

public class B : A
{
    public override void F()
    {
        Console.WriteLine("B");
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot overload base class method in derived class in C++

From Java

Why does calling a method in my derived class call the base class method?

From Dev

Override the behavior of base class private member in a derived class, C++

From Dev

C# interface method declared in Base class need not be again implemented in Derived class

From Dev

Prevent calling base class implemented interface method in the derived class C#

From Dev

C# interface method declared in Base class need not be again implemented in Derived class

From Dev

c# Is there a way that my base class and derived class can implement Interface methods separately?

From Dev

c# Is there a way that my base class and derived class can implement Interface methods separately?

From Dev

can a derived class corrupt base class's implementation

From Dev

Why is there no Seed method to override in my Migration class in C# EF?

From Dev

How to tell which derived class calling override base method

From Dev

C++ base and derived class method trouble

From Dev

is there a way in c++ to have derived classes override a base class static method?

From Dev

C++ Cannot call base class method from within derived class

From Dev

Why doesn't derived templated class's function override the non-templated base class's pure virtual function?

From Dev

Cannot add a derived class object to a list of it's base class type

From Dev

c++ - accessing derived class method via abstract template base class interface pointer, without explicit type in interface

From Dev

Why is it not possible (in scala) to provide implementation for an abstract override method in the implementing base class

From Dev

Cannot Instantiate abstract class but class is not abstract / derived method's param

From Dev

Why isn't my derived class method calling the overriden method from base class despite using the super keyword?

From Dev

Conversion from derived class to it's base class is widening? Why?

From Dev

c# base class method returns derived class

From Dev

base class implementing base interface while derived/concrete class implementing extended interface, why?

From Dev

Using Derived Class In a Base Method

From Dev

C#: Define methods implementation in base class and properties in derived classes

From Dev

C#: Define methods implementation in base class and properties in derived classes

From Dev

C++ Inheritance: Derived class pointer to a Base class invokes Derived class method

From Dev

How do I override `toString` in my derived class and use the private instance variables from the base class?

From Dev

Derived class cannot be transformed into base class in assignment

Related Related

  1. 1

    Cannot overload base class method in derived class in C++

  2. 2

    Why does calling a method in my derived class call the base class method?

  3. 3

    Override the behavior of base class private member in a derived class, C++

  4. 4

    C# interface method declared in Base class need not be again implemented in Derived class

  5. 5

    Prevent calling base class implemented interface method in the derived class C#

  6. 6

    C# interface method declared in Base class need not be again implemented in Derived class

  7. 7

    c# Is there a way that my base class and derived class can implement Interface methods separately?

  8. 8

    c# Is there a way that my base class and derived class can implement Interface methods separately?

  9. 9

    can a derived class corrupt base class's implementation

  10. 10

    Why is there no Seed method to override in my Migration class in C# EF?

  11. 11

    How to tell which derived class calling override base method

  12. 12

    C++ base and derived class method trouble

  13. 13

    is there a way in c++ to have derived classes override a base class static method?

  14. 14

    C++ Cannot call base class method from within derived class

  15. 15

    Why doesn't derived templated class's function override the non-templated base class's pure virtual function?

  16. 16

    Cannot add a derived class object to a list of it's base class type

  17. 17

    c++ - accessing derived class method via abstract template base class interface pointer, without explicit type in interface

  18. 18

    Why is it not possible (in scala) to provide implementation for an abstract override method in the implementing base class

  19. 19

    Cannot Instantiate abstract class but class is not abstract / derived method's param

  20. 20

    Why isn't my derived class method calling the overriden method from base class despite using the super keyword?

  21. 21

    Conversion from derived class to it's base class is widening? Why?

  22. 22

    c# base class method returns derived class

  23. 23

    base class implementing base interface while derived/concrete class implementing extended interface, why?

  24. 24

    Using Derived Class In a Base Method

  25. 25

    C#: Define methods implementation in base class and properties in derived classes

  26. 26

    C#: Define methods implementation in base class and properties in derived classes

  27. 27

    C++ Inheritance: Derived class pointer to a Base class invokes Derived class method

  28. 28

    How do I override `toString` in my derived class and use the private instance variables from the base class?

  29. 29

    Derived class cannot be transformed into base class in assignment

HotTag

Archive