2

This was a question asked to me in one of the interview. There is an interface "Iinterface" and 5 classes which implements this interface. Now i want to implement a new method MethodNew() only inside class3. What is the best approch for this

interface Iinterface()
{
    public void Method1();
}

class Class1 : Iinterface
{
    public void Method1()
    {
    }
}

class Class2 : Iinterface
{
    public void Method1()
    {
    }
}

class Class3 : Iinterface
{
    public void Method1()
    {
    }

    //MethodNew() should be written here
}

class Class4 :Iinterface
{
    public void Method1()
    {
    }
}

class Class5 :Iinterface
{
    public void Method1()
    {
    }
}
MindSwipe
  • 7,193
  • 24
  • 47
Vivi
  • 63
  • 5

3 Answers3

3

If that new method needs to be part of an interface, then the correct way is to create another interface containing that method signature and implement it only in class3. This practice advocates the interface segregation principle from the SOLID.

interface IinterfaceNew
{
  void MethodNew();
}

class Class3 : Iinterface, IinterfaceNew
{
 public void Method1() {}
 public void MethodNew() {}
}

If not, then just go ahead and write a normal method in the class3 :)

PS. Interface method signatures don't have access modifieres so you cannot have Public in the body of interface.

Siavash Rostami
  • 1,883
  • 4
  • 17
  • 31
1

Two possible answers depending on the context.

In new versions check this: https://devblogs.microsoft.com/dotnet/default-implementations-in-interfaces/

A big impediment to software evolution has been the fact that you couldn’t add new members to a public interface. You would break existing implementers of the interface; after all they would have no implementation for the new member!

Default implementations help with that. An interface member can now be specified with a code body, and if an implementing class or struct does not provide an implementation of that member, no error occurs. Instead, the default implementation is used. There are default implementations for interfaces.

You can also change your interface to an abstract class which in most languages and older c# implementations is the way to go. In essence the interface is a pure abstract class. Difference here is that you can't have multiple inheritance from abstract classes and classes in general. Only interfaces allow that.

Another way is to implement inside the class, but then you won't be able to call it in an interface.

And lastly to create a new interface. Since the new method does not apply to all, then it might not make sense to include in the same interface.

Community
  • 1
  • 1
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
0

You don't need to change the interface. Just add the new method to Class3 only.

interface Iinterface()
{
    public void Method1();
}

class Class1 : Iinterface
{
    public void Method1()
    {
    }
}

class Class2 : Iinterface
{
    public void Method1()
    {
    }
}

class Class3 : Iinterface
{
    public void Method1()
    {
    }

    public void MethodNew()
    {
    }
}

class Class4 :Iinterface
{
    public void Method1()
    {
    }
}

class Class5 :Iinterface
{
    public void Method1()
    {
    }
}
PigSpider
  • 881
  • 9
  • 18