C# | How to use Interface References
Last Updated :
11 Jun, 2019
In C#, you are allowed to create a reference variable of an interface type or in other words, you are allowed to create an interface reference variable. Such kind of variable can refer to any object that implements its interface. An interface reference variable only knows that methods which are declared by its interface declaration.
It does not allow accessing any other variables or methods that might be supported by the objects. This concept is similar when you use a parent class reference to access a child class object.
Below are the examples to illustrate the concept of Interface References:
Example 1:
CSharp
// C# program to illustrate the
// concept of Interface References
using System;
// interface declaration
public interface Race {
// declaration of abstract methods of
// interface that will be implemented
// by the class which inherits the interface
void Speed(int s);
void Distance(int d);
}
// class implementing interface
public class Person1 : Race {
int sp1, di1;
// abstract method of
// Race interface
public void Speed(int p1s) {
sp1 = p1s;
Console.WriteLine("Speed Method implemented by Person1");
}
// abstract method of
// Race interface
public void Distance(int p1d) {
di1 = p1d;
Console.WriteLine("Distance Method implemented by Person1");
}
// method of class Person1
public void display1() {
Console.WriteLine("The Speed of 1st person is: "+sp1);
Console.WriteLine("The distance covered by 1st person is: "+di1);
}
}
// class implementing interface
public class Person2 : Race {
int sp2, di2;
// abstract method of
// Race interface
public void Speed(int p2s) {
sp2 = p2s;
Console.WriteLine("Speed Method implemented by Person2");
}
// abstract method of
// Race interface
public void Distance(int p2d) {
di2 = p2d;
Console.WriteLine("Distance Method implemented by Person2");
}
// method of class Person2
public void display2() {
Console.WriteLine("The Speed of 2nd person is: "+sp2);
Console.WriteLine("The distance covered by 2nd person is: "+di2);
}
}
// Driver Class
public class GFG {
// Main method
public static void Main(String []args) {
// creating an instance of Person1 class
Person1 obj1 = new Person1();
// creating an instance of Person2 class
Person2 obj2 = new Person2();
// creating an Reference
// of interface Race
Race r;
// ----- For Person1 Class ----------
// assigning Person1 object 'obj1'
// to interface Reference 'r'
r = obj1;
// Now you can access the abstract method
// of Race interface which are implemented
// by class Person1
r.Speed(10);
r.Distance(50);
// if you will try to call display1()
// method using 'r' it will give error
//r.display1();
// calling the display1()
// method of Person1 Class
obj1.display1();
// ----- For Person2 Class ----------
// assigning Person2 object 'obj2'
// to interface Reference 'r'
r = obj2;
// Now you can access the abstract method
// of Race interface which are implemented
// by class Person2
r.Speed(15);
r.Distance(45);
// if you will try to call display2()
// method using 'r' it will give error
//r.display2();
// calling the display1()
// method of Person1 Class
obj2.display2();
}
}
Output:
Speed Method implemented by Person1
Distance Method implemented by Person1
The Speed of 1st person is: 10
The distance covered by 1st person is: 50
Speed Method implemented by Person2
Distance Method implemented by Person2
The Speed of 2nd person is: 15
The distance covered by 2nd person is: 45
Explanation: In above example, we have an interface named
Race an two classes
Person1 and
Person2 which are implementing the methods of the interface. The
Person1 class has its own method named
display1() and similar
Person2 class its own method
display2() which cannot be called by using interface reference. In order to call the methods using interface reference(here
r is interface reference), you have to assign to class object to it. Like if you are assigning Person1's object
obj1 to
r i.e.
r = obj1; then you call the
Speed() and
Distance() methods that are implemented by the
Person1 class. In order to call
display1() method, you must have to use obj1. Similarly using
r = obj2; we are calling methods of
Person2 class.
Example 2:
CSharp
// C# program to illustrate the
// concept of Interface References
using System;
// interface declaration
interface Vehicle {
// all are the abstract methods.
void changeGear(int a);
void speedUp(int a);
void applyBrakes(int a);
void printStates();
}
// class implements interface
class Bicycle : Vehicle {
int speed;
int gear;
// to change gear
public void changeGear(int newGear)
{
gear = newGear;
}
// to increase speed
public void speedUp(int increment)
{
speed = speed + increment;
}
// to decrease speed
public void applyBrakes(int decrement)
{
speed = speed - decrement;
}
public void printStates()
{
Console.WriteLine("speed: " + speed + " gear: " + gear);
}
}
// Driver Class
class GFG {
// Main Method
public static void Main(String[] args)
{
// creating an instance of Bicycle
Bicycle bicycle = new Bicycle();
// Creating interface references
Vehicle obj;
// assigning Bicycle object 'bicycle'
// to interface Reference 'obj'
obj = bicycle;
// calling the abstract methods
// implemented by class Bicycle
obj.changeGear(4);
obj.speedUp(5);
obj.applyBrakes(2);
Console.WriteLine("Bicycle Present State:");
// calling the method of class Bicycle
obj.printStates();
}
}
Output:
Bicycle Present State:
speed: 3 gear: 4
Explanation: In the above example, Vehicle is an interface and Bicycle class implements this interface. Here
obj is declared to be a reference to Vehicle interface in the Main() method. Now this
obj is used to refer the object bicycle of the Bicycle class.
Similar Reads
C# Tutorial C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET Framework The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# Interview Questions and Answers C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications.Her
15+ min read
C# Dictionary Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature mean
5 min read
C# List Class In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.Generic namespace. List class can be used to create a collection of different types like integers, strings, etc. List<T> class also provides the methods to search,
7 min read
C# Delegates A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form (Windows Form application),
6 min read
ASP.NET Interview Questions and Answer ASP.NET is a popular framework by Microsoft for building fast and scalable web applications. It allows developers to create dynamic websites, services, and apps, using server-side code and offering a user-friendly experience. Trusted by companies like Microsoft, Dell, and Accenture, ASP.NET is used
15+ min read
C# .NET Framework (Basic Architecture and Component Stack) C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Data Types Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# Arrays An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read