1

I am confused with this scenario of an abstract class and interface having same signature method. How many definitions will be there in deriving class? How will the call be resolved?

public abstract class AbClass
{
    public abstract void printMyName();
}

internal interface Iinterface
{
    void printMyName();
}

public class MainClass : AbClass, Iinterface
{
    //how this methods will be implemented here??? 
}
Neha
  • 753
  • 1
  • 5
  • 18
  • You have to use explicit method overriding. There will be maximum two definitions only. For example : 1st method. `AbClass.printMyName() { console.writeln("I am AbClass") };`. 2nd Method : `Iinterface.printMyName() { console.writeln("I am Iinterface") };` – Sandeep Kushwah Sep 04 '15 at 07:07

2 Answers2

4

There will be just one implementation in default scenario, but you can have two implementation if you'll define method with void Iinterface.printMyName signature. Take a look at SO question about Difference between Implicit and Explicit implementations. Also you have some errors in your sample

  • printMyName in AbClass is not marked as abstract, therefore it should have body.
  • if you want to have abstract method - it can't be private

-

public abstract class AbClass
{
    public abstract void printMyName();
}

internal interface Iinterface
{
    void printMyName();
}

public class MainClass : AbClass, Iinterface
{
    //how this methods will be implemented here??? 
    public override void printMyName()
    {
        Console.WriteLine("Abstract class implementation");
    }

    //You can implement interface method using next signature
    void Iinterface.printMyName()
    {
        Console.WriteLine("Interface implementation");
    }
}

public class MainClass_WithoutExplicityImplementation : AbClass, Iinterface
{
    //how this methods will be implemented here??? 
    public override void printMyName()
    {
        Console.WriteLine("Abstract class and interface implementation");
    }
}

Example of usage

var mainInstance = new MainClass();
mainInstance.printMyName();      //Abstract class implementation
Iinterface viaInterface = mainInstance;
viaInterface.printMyName();      //Interface implementation


var mainInstance2 = new MainClass_WithoutExplicityImplementation();
mainInstance2.printMyName();      //Abstract class and interface implementation
Iinterface viaInterface = mainInstance2;
viaInterface.printMyName();      //Abstract class and interface implementation
Community
  • 1
  • 1
Artiom
  • 7,694
  • 3
  • 38
  • 45
0

You can ommit the implementation of the interface within your concrete class, as the base class already implements it. However you might also implement the interface explicitly meaning you may "override" the behaviour from your base (the-abstract) class (overriding is not real the correct word here). This further expects to cast your instance explicitky to the interface to call that method:

public class MainClass : AbClass, Iinterface
{
    //how this methods will be implemented here??? 
    void Iinterface.printMyName()
    {
        throw new NotImplementedException();
    }
}

You may call this cia ((Iinterface(myMainClassInstance).printMyName(). If you call myMainClassInstance.printMyName however the base-implementation is called.

If you want to support a base-implementatation within your base-class you may however make the method virtual and override it within your derived class.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111