C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Introduction
In the previous chapter, we have studied on
interfaces and
explicit interface implementations with examples. Now let's study how to implement interface members virtually and how to re-implement interfaces in the derived classes.
Objective
The main objective of this article is to learn about
virtual implementation and re-implementation of interfaces in
C# with examples.
Virtual Implementation of Interfaces
We already know that, any
implicit implemented types of an interface are
sealed by default. In order to override any type or member of an interface, it must be called with
virtual or
abstract in the base
class.
Let's have an example,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example
{
class Program
{
interface IExample
{
void Start();
}
class First : IExample
{
public virtual void Start()
{
Console.WriteLine("This is from Base class");
}
}
class Second : First
{
public override void Start()
{
Console.WriteLine("This is from Derived class");
}
}
static void Main()
{
Second app = new Second();
app.Start(); // Calls derived class implementation
((First)app).Start(); // This also calls the derived class implementation
((IExample)app).Start(); // Calls derived class implementation
}
}
}
In the above code, we have an interface
IExample with an
abstract method
Start()
. At first we are virtually implementing
Start()
in the base
class First. And from that, we can override that
Start()
method in the derived class
Second by using
override modifier.
Now the important thing is that, we have created an object 'app' to the class Second in the Main method. it will directly calls the derived class implementation.
And in the next line, we are calling the
Start()
of base class (
First)
explicitly by using
cast. Still the derived class (
Second) implementation is called as we are overriding the same in the derived class.
Overridden method is given the most priority even we
cast the
interface (
IExample).
Now check the conditions by running the above code in your console which prints following lines,
Re-implementing interfaces
Re-implementing means re-using the
interface in the derived classes even though it is already implemented by the base class.
When we call through the interface, re-implementation hijacks the member implementation.
- It checks whether the member in a base class is virtual or not.
- It also checks whether the implementation of a member is implicit or explicit.
Let's have an example of re-implementing interface in the derived (sub) class,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example
{
class Program
{
interface IExample // Interface
{
void Start();
}
class First : IExample
{
void IExample.Start() // Explicit interface implementation
{
Console.WriteLine("This is IExample's First.Start");
}
}
class Second : First, IExample
{
public new void Start()
{
Console.WriteLine("This is Second.Start");
}
}
static void Main()
{
Second app = new Second();
app.Start(); // Calls Second.Start
((IExample)app).Start(); // Calls Second.Start
}
}
}
Now, re-implementing comes into the picture. In order to override the base class (
First) method, we have to re-implement
IExample's
Start()
method at the derived
class (
Second) like we did in the above code. This will let the object
app to call the derived class's implementation even we use interface casting.
The above code print the following lines in your console,
Now, let's that we are using implicit implementation for the same definition instead of explicit interface implementation:
Let's recall the above code after a slight change,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example
{
class Program
{
interface IExample // Interface
{
void Start();
}
class First : IExample
{
public void Start() // Implicit interface implementation
{
Console.WriteLine("This is IExample's First.Start");
}
}
class Second : First, IExample
{
public new void Start()
{
Console.WriteLine("This is Second.Start");
}
}
static void Main()
{
Second app = new Second();
app.Start(); // Calls Second.Start
((IExample)app).Start(); // Calls Second.Start
((First)app).Start(); // Calls First.Start
}
}
}
The above code is almost similar to the example we seen previously. Only change in here is that, we are implementing the
IExample's property implicitly in the base
class instead of doing it explicitly.
By using this approach, we can have a way to call the base class's (First) Start()
method that we did in the Main method.
Run this code in your console and observe that base class implementation is called by using cast (explicitly),
There are some limitations with
interface re-implementations. They are,
- We cannot call a base class method from its derived class (No way to do that).
- The base class members doesn't think on the method that is to be re-implemented in the derived class and may not allow for potential consequences.
In order to overcome this limitations, better to mark a base class member (implicit) as virtual and then override it in the derived class .
Or if you need implement it explicitly, then follow the below approach
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example
{
class Program
{
interface IExample // Interface
{
void Start();
}
class First : IExample // Base class
{
void IExample.Start() // Explicit interface implementation
{
Start();
}
public virtual void Start() // Virtual method
{
Console.WriteLine("This is IExample's First.Start");
}
}
class Second : First // Derived class
{
public override void Start() // Overriding virtual method
{
Console.WriteLine("This is Second.Start");
}
}
static void Main()
{
Second app = new Second();
app.Start(); // Calls Second.Start
((IExample)app).Start(); // Calls Second.Start
((First)app).Start(); // Calls Second.Start
}
}
}
In the above example, we have explicitly implemented an interface member and re-initializing it with a
virtual modifier.
With that we can easily override the base class method.
Output of this code will be,
Conclusion
In this article, we have learnt
virtual interfaces and re-implementing interfaces in
C# programming. Hope you understand.
Thanks for reading.
Regards,
Krishna.