Inheriting From Classes and Implementing Interfaces: Use Inheritance To Create New Reference Types
Inheriting From Classes and Implementing Interfaces: Use Inheritance To Create New Reference Types
Unit 7: Inheriting From Classes and Implementing Interfaces This module explains how to
use inheritance to create new reference types
What is an Interface?
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no
implementation; it only has the signature or in other words, just the definition of the methods without the
body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all
subclasses or it defines specific set of methods and their arguments. The main difference between them is
that a class can implement more than one interface but can only inherit from one abstract class. Since C#
doesn't support multiple inheritance, interfaces are used to implement multiple inheritance.
When we create an interface, we are basically creating a set of methods without any implementation that
must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a
part of two classes: one from inheritance hierarchy and one from the interface.
When we create an abstract class, we are creating a base class that might have one or more completed
methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of
an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to
provide a base class definition for how a set of derived classes will work and then allow the programmers to
fill the implementation in the derived classes.
What is an Interface?
An interface looks like a class but has got no implementation. In an interface we cannot do any
implementation but we can declare signatures of properties, methods, delegates and events.
Implementation part is been handle by the class that implements the interface. Implemented interface
enforces the class like a standard contract to provide all implementation of interface members.
We can implement single interface or multiple interfaces to the class. By default interfaces are public.
We declare interface by using "interface" keyword.
Feature
Interface
Abstract class
Multiple
inheritance
Default
implementation
Access
Modfiers
Core VS
Peripheral
Homogeneity
Speed
Fast
Adding
functionality
(Versioning)
Fields and
Constants
interface IEmployee{
void DisplayEmployeeDetails();
}
Above syntax shows simple demonstration of interface "IEmployee" with signature of a method
"DisplayEmployeeDetails". Now let's implement the same interface to a class.
class clsEmployee : IEmployee
{
{
public void DisplayEmployeeDetails(){
Console.WriteLine(Displaying employee details);
}
}
As you see in our above snippet of code that implementation of an interface method signature is done by the
class which implemented that interface.
When you put a dot operator after an interface name you will see all the signatures defined in an interface.
When we have two different interfaces with same method name then in that scenario we can use explicit
interface implementation.
Example of Explicit Interface
public interface IEmployee{
void DisplayEmployeeDetails();
}
public interface ICompany{
void DisplayEmployeeDetails();
}
In the above code we have two different interfaces "IEmployee" and "ICompany" with same method name.
Now let's implement these two interfaces to a class "clsEmployee" as shown in below snippet of code.
public class clsEmployee : IEmployee,ICompany
{
//class code goes here
}
In order to implement an interface explicitly we have to define the interface name followed by (".") dot
operator before method name as shown in below snipped code.
public class Employee : IEmployee,ICompany
{
void ICompany.DisplayEmployeeDetails(){
Console.WriteLine("ICompany Employee Name --> Questpond and Employee Code --> 009");
}
void IEmployee.DisplayEmployeeDetails(){
Console.WriteLine("IEmployee Employee Name --> Questpond and Employee Code --> 009");
}
}
Console Application
Finally let's create the objects of two interfaces "IEmployee" and "ICompany" in our main method of a
Console Application program.
class Program{
static void Main(string[] args)
{
IEmployee IEmp = new clsEmployee();
IEmp.DisplayEmployeeDetails();
ICompany IComp = new clsEmployee();
IComp.DisplayEmployeeDetails();
}
}
Let's run the application (Ctrl + F5) and display the output as shown it below.
When you put a colon after a class name you will see the custom defined and as well as system defined
interfaces in the program.
Above code shows simple demonstration of an interface "IEmployee" with signature of a method
"DisplayEmployeeDetails".
Now let's create a class "Employee" and implement the interface implicitly as shown in below snippet of
code.
public class Employee : IEmployee
{
public void DisplayEmployeeDetails()
{
Console.WriteLine("Employee Name --> Questpond and Employee Code --> 009");
}
}
Console Application
Finally let's create the objects of an interface "IEmployee" in our main method of a Console Application
program.
public class Program{
static void Main(string[] args)
{
IEmployee IEmp = new clsEmployee();
IEmp.DisplayEmployeeDetails();
}
}
Let's run the application (Ctrl + F5) and display the output as shown it below.