C # interface inheritance for abstract class

Suppose I have an interface as defined below:

public interface IFunctionality { void Method(); } 

and I implement this interface for an abstract class, as shown below:

 public abstract class AbstractFunctionality: IFunctionality { public void Method() { Console.WriteLine("Abstract stuff" + "\n"); } } 

again, I have a specific class that inherits from an abstract class, as shown below:

 public class ConcreteFunctionality: AbstractFunctionality { public void Method() { Console.WriteLine("Concrete stuff" + "\n"); } } 

Now I have the following code,

 ConcreteFunctionality mostDerived = new ConcreteFunctionality(); AbstractFunctionality baseInst = mostDerived; IFunctionality interfaceInst = mostDerived; mostDerived.Method(); baseInst.Method(); interfaceInst.Method(); 

The result that I get after doing this material is as follows.

 Concrete stuff Abstract stuff Abstract stuff 

But what I expected the output would be "Concrete Stuff" in all three cases, because what I am doing here assigns the ConcreteFunctionality reference to variables of type AbstractFunctionality and IFunctionality .

What is happening inside the country. Please clarify.

+50
c # oop interface abstract-class concrete
Jan 25 '13 at 7:03
source share
3 answers

Here:

 public class ConreteFunctionality:AbstractFunctionality { public void Method() { Console.WriteLine("Concrete stuff" + "\n"); } } 

... you are not overriding the existing method. You create a new method that hides the existing one. (You should also receive a warning prompting you to use the new modifier if you really want to.) The interface was implemented in AbstractFunctionality , so the interface mapping table references the method in this class.

Now, if you redefine the interface:

 public class ConcreteFunctionality : AbstractFunctionality, IFunctionality 

... then the interface mapping will refer to the method in ConcreteFunctionality , and you will get the behavior that you expect to call through the interface (i.e. your third call), but you will still get the implementation in AbstractFunctionality for your second call.

It would be cleaner and safer to make the method in AbstractFunctionality virtual and override it in ConcreteFunctionality . Thus, he will use the ConcreteFunctionality implementation in all cases.

+74
Jan 25 '13 at 7:05
source share

You need to define classes as:

 public abstract class AbstractFunctionality:IFunctionality { public virtual void Method() { Console.WriteLine("Abstract stuff" + "\n"); } } public class ConreteFunctionality:AbstractFunctionality { public override void Method() { Console.WriteLine("Concrete stuff" + "\n"); } } 

Since you did not redefine method () in ConreteFunctionality, the runtime executes method () associated with the AbstractFunctionality object, since it cannot apply dynamic polymorphism here. The introduction of virtual and override forces the runtime to execute the override method in the child class.

+30
Jan 25 '13 at 7:05
source share

You are missing the virtual and override keywords. Without this, you will not get virtual functions.

You can mark Method in AbstractFunctionality as virtual and mark Method in ConreteFunctionality as override . As mihir showed.

Similar problems are addressed in Why are C # interface methods not declared abstract or virtual?

+14
Jan 25 '13 at 7:04
source share



All Articles