Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing few methods of a interface class-C#

Tags:

c#

interface

Is it possible in C# to have a class that implement an interface that has 10 methods declared but implementing only 5 methods i.e defining only 5 methods of that interface??? Actually I have an interface that is implemented by 3 class and not all the methods are used by all the class so if I could exclude any method???

I have a need for this. It might sound as a bad design but it's not hopefully. The thing is I have a collection of User Controls that needs to have common property and based on that only I am displaying them at run time. As it's dynamic I need to manage them for that I'm having Properties. Some Properties are needed by few class and not by all. And as the control increases this Properties might be increasing so as needed by one control I need to have in all without any use. just the dummy methods. For the same I thought if there is a way to avoid those methods in rest of the class it would be great. It sounds that there is no way other than having either the abstract class or dummy functions :-(

like image 937
Jankhana Avatar asked Apr 27 '10 13:04

Jankhana


People also ask

How do you implement an interface method in C#?

The entities that implement the interface must provide the implementation of declared functionalities. In C#, an interface can be defined using the interface keyword. An interface can contain declarations of methods, properties, indexers, and events. However, it cannot contain fields, auto-implemented properties.

Do you have to implement all methods of an interface C#?

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class. Implement every method defined by the interface.

What kind of methods can be implemented in an interface?

The interface body can contain abstract methods, default methods, and static methods. An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation).

How interface methods are implemented?

It cannot have a method body. Java Interface also represents the IS-A relationship. Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body). Interfaces specify what a class must do and not how.


3 Answers

You can make it an abstract class and add the methods you don't want to implement as abstract methods.

In other words:

public interface IMyInterface {     void SomeMethod();     void SomeOtherMethod(); }  public abstract class MyClass : IMyInterface {     // Really implementing this     public void SomeMethod()     {         // ...     }      // Derived class must implement this     public abstract void SomeOtherMethod(); } 

If these classes all need to be concrete, not abstract, then you'll have to throw a NotImplementedException/NotSupportedException from inside the methods. But a much better idea would be to split up the interface so that implementing classes don't have to do this.

Keep in mind that classes can implement multiple interfaces, so if some classes have some of the functionality but not all, then you want to have more granular interfaces:

public interface IFoo {     void FooMethod(); }  public interface IBar() {     void BarMethod(); }  public class SmallClass : IFoo {     public void FooMethod() { ... } }  public class BigClass : IFoo, IBar {     public void FooMethod() { ... }     public void BarMethod() { ... } } 

This is probably the design you really should have.

like image 179
Aaronaught Avatar answered Sep 25 '22 02:09

Aaronaught


Your breaking the use of interfaces. You should have for each common behaviour a seperate interface.

like image 25
RvdK Avatar answered Sep 24 '22 02:09

RvdK


That is not possible. But what you can do is throw NotSupportedException or NotImplementedException for the methods you do not want to implement. Or you could use an abstract class instead of an interface. That way you could provide a default implementation for methods you choose not to override.

public interface IMyInterface
{
  void Foo();

  void Bar();
}

public class MyClass : IMyInterface
{
  public void Foo()
  {
    Console.WriteLine("Foo");
  }

  public void Bar()
  {
    throw new NotSupportedException();
  }
}

Or...

public abstract class MyBaseClass
{
  public virtual void Foo()
  {
    Console.WriteLine("MyBaseClass.Foo");
  }

  public virtual void Bar()
  {
    throw new NotImplementedException();
  }
}

public class MyClass : MyBaseClass
{
  public override void Foo()
  {
    Console.WriteLine("MyClass.Foo");
  }
}
like image 30
Brian Gideon Avatar answered Sep 23 '22 02:09

Brian Gideon