1

I have an interface, 2 class

public interface ITest  
{  
 void Method1(){}  
 void Method2(){}   
}  
public class Test1 : ITest  
{  
 //Just implement Method1() (how can I just implement Method1() without implementing Method2()?)
 public string Method1()  
 {...}  
}  
public class Test2 : ITest  
{  
 //Implement both Method1() & Method2()
 public string Method1()  
 {...}  
}  

For the request, I must add more method called Method3() to the interface, my interface should be:

public interface ITest  
{  
 void Method1(){}  
 void Method2(){}    
 void Method3(){}   
}  

Must I add Method3() to both class Test1 & Test2, can I just add when I need.
Many Thanks

Arnaud F.
  • 8,252
  • 11
  • 53
  • 102
akari
  • 149
  • 9
  • 2
    A class implementing an interface needs to implement all methods, if not you need to check for abstract class maybe thats what you need ! – V4Vendetta Oct 05 '11 at 06:31
  • Take a look at: http://stackoverflow.com/questions/5296021/what-is-different-between-an-abstract-and-an-interface-class-in-c – drneel Oct 05 '11 at 06:32

4 Answers4

3

You should be using abstract base class instead of an interface and make the methods as virtual. In your derived classes you should be able to override only those that make sense.

However you can also consider separating the interfaces since its not wise to have an interface or a base class of which only some methods are relevant to some derived classes.

You can chose to throw exceptions in unimplemented methods but that should be the last resort.

Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131
  • 2
    if the methods don't make sense the interface probably do too much. You may want to split the interface in two. – Rickard Oct 05 '11 at 06:36
1

I would say that if you don't implement all methods then your class is not of type ITest. I would write your code as follows:

public interface ITest1
{
    void Method1();
}
public class interface ITest2 : ITest1
{
    void Method2();
}
public class Test1 : ITest1
{
    public void Method1() {}
} 
public class Test2 : ITest2
{
    public void Method1() {}
    public void Method2() {}
} 
Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
0

An interface must be fully implemented by its classes.

However, what you can do is to create a new interface with a name that better expresses what it is, with the Method3 with another name that better expresses its intent.

And then have the classes that should expose this functionality implement the new interface.

public interface IDescriptionOfWhatIAm
{
    void MethodNameWithGoodDescription();
}

public class Test1 : IDescriptionOfWhatIAm
{
    public void MethodNameWithGoodDescription() {}
}

Read more about the interface segregation principle and SOLID principles.

Rickard
  • 2,325
  • 13
  • 22
0

You have to implement Method 2, but you can implement it explicitly. However its a little ugly.

public interface ITest  
{  
 void Method1(){}  
 void Method2(){}   
}  
public class Test1 : ITest  
{  
 //Just implement Method1() (how can I just implement Method1() without implementing             Method2()?)
 public string Method1()  
 {...}

    private void ITest.Method2(){ throw new NotSupportedException(); }    
}  
public class Test2 : ITest  
{  
 //Implement both Method1() & Method2()
 public string Method1()  
 {...}  
}  
m3kh
  • 7,881
  • 2
  • 31
  • 37