Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Interface Inheritance to Abstract class

Suppose if 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 concrete class which Inherits from abstract class as 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 output which I am getting after execution of this stuff is as following.

Concrete stuff
Abstract stuff
Abstract stuff

But what I have been expecting the output to be "Concrete Stuff" in all the three cases as what I am doing here is assigning the reference of ConcreteFunctionality to the variables of type AbstractFunctionality and IFunctionality.

What is happening internally. Kindly clarify.

like image 651
Vikram Avatar asked Jan 25 '13 07:01

Vikram


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


3 Answers

Here:

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

... you're not overriding the existing method. You're creating a new method which hides the existing one. (You should get a warning, too, suggesting the use of the new modifier if you really want this behaviour.) The interface was implemented in AbstractFunctionality, so the interface mapping table refers to the method in that class.

Now if you reimplement the interface:

public class ConcreteFunctionality : AbstractFunctionality, IFunctionality

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

It would be generally cleaner and more sanity-preserving to make the method in AbstractFunctionality virtual, and override it in ConcreteFunctionality. That way it will use the ConcreteFunctionality implementation in all cases.

like image 142
Jon Skeet Avatar answered Sep 22 '22 10:09

Jon Skeet


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");
    }
}

As you have not overriden Method() in ConreteFunctionality, the run time environment executes Method() associated with AbstractFunctionality object as it cannot apply dynamic polymorphism here. Introducing virtual and override makes the run time environment to execute the overriden method in child class.

like image 35
mihirj Avatar answered Sep 20 '22 10:09

mihirj


You are missing virtual and override keywords. Without this you don't get virtual functions.

You can mark Method in AbstractFunctionality as virtual and mark the Method in ConreteFunctionality as override. As mihirj has shown.

Similar issues tackled in - Why are C# interface methods not declared abstract or virtual?

like image 22
Karthik T Avatar answered Sep 23 '22 10:09

Karthik T