Interfaces
Interfaces represent one of the fundamental concepts of object-oriented programming. You can think of them as completely abstract classes whose members are all abstract and public. Interfaces contain definitions for a group of related functionalities that classes must implement. It is often said that interfaces represent “contracts” that classes must implement.
Interfaces contain only the definitions of their members, not their implementation, which is the core concept of working with interfaces. They do not contain any fields, not even static ones. They do not contain constructors or destructors (finalizers). Because all interface members are public, you cannot use access modifiers within an interface. Finally, you cannot define any types inside an interface.
An interface name must be a valid identifier and, by convention, should start with a capital letter I. To declare an interface, instead of the keyword class, you use the keyword interface. For example:
interface IPerson
{
string Name(string name);
}In this interface, the method Name() is declared without access modifiers, where the method body is replaced with a semicolon ;. Because interfaces do not contain constructors, you cannot instantiate objects directly from them! You can only use interfaces to derive classes or other interfaces. For example, a class implementing the interface IPerson can look like this:
class Person : IPerson
{
public string Name(string name)
{
return name;
}
}Thus, class Person implements interface IPerson, meaning it inherits the interface IPerson and implements all of its members.
When you create a class that implements an interface, the class must provide methods that match those defined in the interface, where: the class method must be public, since all interface methods are implicitly public; the return type must match that of the interface method; the method name must match the interface method name; and parameters, if any, must match the parameter types in the interface method.
Besides methods, interfaces can also contain properties, indexers, and events. If an interface contains a property, the class must implement at least one of the accessors: get or set. All interface members are implicitly public, abstract, and virtual.
Suppose the task is to create an interface IPerson with a method Name(), and classes Teacher and Student that must implement that interface. It can look, for example, like this:
using System;
interface IPerson
{
string Name(string name);
}
class Teacher : IPerson
{
public string Name(string name)
{
name = "Teacher: " + name;
return name;
}
}
class Student : IPerson
{
public string Name(string name)
{
name = "Student: " + name;
return name;
}
}
class Program
{
static void Main()
{
Student s = new Student();
Console.WriteLine(s.Name("Petar Petrovic"));
Teacher t = new Teacher();
Console.WriteLine(t.Name("Velimir Radlovacki"));
}
}
In this simple example, both classes implemented the interface IPerson according to the specified rules: the Name() method is public, the return type string matches the interface method return type, the method name Name() matches the interface, and the parameter string name matches the interface method parameter.
Overriding Implemented Methods
If you need to override a base class method in a derived class where that method was implemented via an interface in the base class, you should declare that method as virtual in the base class and override it in the derived class. For example:
using System;
interface IPerson
{
string Name(string name);
}
class Person : IPerson
{
public virtual string Name(string name)
{
return name;
}
}
class Teacher : Person
{
public override string Name(string name)
{
return "Teacher: " + name;
}
}
class Program
{
static void Main()
{
Person p = new Person();
Console.WriteLine(p.Name("Velimir Radlovacki"));
Teacher t = new Teacher();
Console.WriteLine(t.Name("Velimir Radlovacki"));
}
}
In this simple example, the virtual method Name() of object p will return just its argument value, i.e. Velimir Radlovacki, whereas the method Name() of object t, which overrides it, will return the string Teacher: concatenated with the argument value, yielding Teacher: Velimir Radlovacki.
Inheritance with Interface Implementation
When deriving a class from another class while also implementing an interface, specify the base class first after the colon, followed by a comma and the base interface.
Suppose class Person implements interface IPerson, and class Teacher inherits from class Person while implementing interface IPerson. For example:
using System;
interface IPerson
{
string Name(string name);
}
class Person : IPerson
{
public string Name(string name)
{
return name;
}
}
class Teacher : Person, IPerson
{
public void TeacherName(string teacherName)
{
Console.WriteLine("Teacher: " + teacherName);
}
}
class Program
{
static void Main()
{
Teacher t = new Teacher();
Console.WriteLine(t.Name("Velimir Radlovacki"));
t.TeacherName("Velimir Radlovacki");
}
}
When defining the class Teacher, the class name Teacher is specified first, then a colon, then the base class Person, then a comma, then the base interface IPerson. Object t of class Teacher can use the public method Name() inherited from class Person, which was implemented according to the definition in interface IPerson.
Inheritance Among Interfaces
Interfaces can inherit from other interfaces. In the following example…
interface IPin
{
string Pin(string pin);
}
interface IPerson : IPin
{
string Name(string name);
}
class Person : IPerson
{
public string Pin(string pin)
{
return pin;
}
public string Name(string name)
{
return name;
}
}…class Person implements interface IPerson, which inherits interface IPin. This means that inside class Person, you must implement everything defined by interface IPerson as well as everything defined by interface IPin.
Multiple Interface Implementation
In earlier lessons, it was noted that C# does not support multiple inheritance, which is true regarding class inheritance. However, C# does support multiple interface implementation, meaning that a single class can implement multiple interfaces.
For example, for teachers, relevant data includes name and personal ID (PIN/JMBG), while for students, name, personal ID, and student ID (UEN/JOB) are required. Teachers do not have a student ID assigned, but students must have one. In such a scenario, three interfaces can be defined: IName, IPin, and IStudentId. The first two are implemented by the Teacher class, while all three are implemented by the Student class:
interface IName
{
string Name(string name);
}
interface IPin
{
string Pin(string pin);
}
interface IStudentId
{
string StudentId(string id);
}
class Teacher : IName, IPin
{
public string Name(string name)
{
return name;
}
public string Pin(string pin)
{
return pin;
}
}
class Student : IName, IPin, IStudentId
{
public string Name(string name)
{
return name;
}
public string Pin(string pin)
{
return pin;
}
public string StudentId(string id)
{
return id;
}
}From everything learned in this lesson, you can conclude that interfaces in C# are an excellent tool for writing well-structured and maintainable code. They enable decoupling definition from implementation, which contributes to greater modularity and code reusability.