1

I have code below as

public interface NomiInterface
{
     void method();
}
public abstract class Nomi1
{
     public void method()
     {
     }
}
public class childe : Nomi1, NomiInterface 
{ 
}

Now compiled successfully? why not need to override the interface method in childe class?

Nomi Ali
  • 2,165
  • 3
  • 27
  • 48
  • 1
    Did you notice that the interface defines a method named `mehtod`, but the abstract class defines `method`? – John Saunders Dec 14 '13 at 07:29
  • Have you tried your newely edited answer? Because it compiles now without any errors. Your problem was what John Saunders wrote. – Kikaimaru Dec 14 '13 at 07:33
  • 1
    You dont need to implement it again because childe class already has "method" method implemented from its parent Nomi1. If you want to implement it in other way, make the method "method" in Nomi1 virtual and override it in childe class – Kikaimaru Dec 14 '13 at 08:24

1 Answers1

4

You need explicit implementation of interface. The abstract class method method() implementation fulfill the need of implementation of abstract method of interface. So define the method of interface in the class childe but explicit implementation need to call the method of interface but not on class.

public interface NomiInterface
{
     void method();
}
public abstract class Nomi1
{
     public void method()
     {
          Console.WriteLine("abstract class method");
     }
}
public class childe : Nomi1, NomiInterface 
{ 
     void NomiInterface.method()
     {
          Console.WriteLine("interface method"); 
     }
}

You can test how you can call the method of abstract class and interface implementation present in childe

childe c = new childe();
NomiInterface ni = new childe();
ni.method();
c.method();

The output is

interface method
abstract class method

On the other hand if you do not do explicit interface implementation then the implementation given in childe class wont be call on childe or interface object.

public interface NomiInterface
{
    void method();
}
public abstract class Nomi1
{
    public void method()
    {
        Console.WriteLine("abstract class method");
    }
}
public class childe : Nomi1, NomiInterface
{
    void method() { Console.WriteLine("interface method"); }
}

Create object of class and interface as we did previously.

childe c = new childe();
NomiInterface ni = new childe();
ni.method();
c.method();

The output you will get

abstract class method
abstract class method

As an additional note you will take care of naming conventions for class / method names. You can find more about naming conventions here.

Adil
  • 146,340
  • 25
  • 209
  • 204
  • 1
    Problem is that he has a typo in __mehtod__ on interface. This answer is just confusing. – Kikaimaru Dec 14 '13 at 07:31
  • @Kikaimaru - OP asked where the implementation is taken from, the abstract class and why is it not overriden. Adil's answer explains this. – Tim Dec 14 '13 at 07:42
  • @Tim Question was edided after my comment. But I still don't understand this answer. Answer is that he doesn't need to implemented it in child because parent already implements it and its not overriden because no one overrides it. Explicit interface implementation doesn't help anything. – Kikaimaru Dec 14 '13 at 08:26