Click here to Skip to main content
16,012,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Look at class B which inherits from absract class Customer and implement the interface CC. Both of these have Method with same name .

it's overriding the OpenGate() method of abstract class.

Then what about the method from the interface.How this class getiing compiled though we are not implementing OpenGate() method of CC interface??

What I have tried:

interface CC
{
void OpenGate();

}


public abstract class Customer
{
public virtual void OpenGate()
{
Console.WriteLine("Abstarct");

}

}
public class B : Customer, CC
{
public override void OpenGate()
{
Console.WriteLine("B");

}



}



public class C : B
{
public override void OpenGate()
{
Console.WriteLine("C");

}



}
Posted
Updated 30-May-16 19:50pm

1 solution

Because the OpenGate method has the same signature as that required by both the abstract class and Interface, it satisfies both requirements so it compiles OK. If you need different methods to be executed, then you need to explicitly implement the interface version:
C#
public class B1 : Customer, CC
    {
    public override void OpenGate()
        {
        Console.WriteLine("B1: Customer");
        }
    void CC.OpenGate()
        {
        Console.WriteLine("B1: CC");
        }
    }
public class B2 : Customer, CC
    {
    public override void OpenGate()
        {
        Console.WriteLine("B2");
        }
    }
Then, you can use them both:
C#
B1 b1 = new B1();
CC cc = b1;
b1.OpenGate();
cc.OpenGate();
B2 b2 = new B2();
cc = b2;
b2.OpenGate();
cc.OpenGate();
And you will get:
B1: Customer
B1: CC
B2
B2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900