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:
using System;
public interface Race {
void Speed( int s);
void Distance( int d);
}
public class Person1 : Race {
int sp1, di1;
public void Speed( int p1s) {
sp1 = p1s;
Console.WriteLine( "Speed Method implemented by Person1" );
}
public void Distance( int p1d) {
di1 = p1d;
Console.WriteLine( "Distance Method implemented by Person1" );
}
public void display1() {
Console.WriteLine( "The Speed of 1st person is: " +sp1);
Console.WriteLine( "The distance covered by 1st person is: " +di1);
}
}
public class Person2 : Race {
int sp2, di2;
public void Speed( int p2s) {
sp2 = p2s;
Console.WriteLine( "Speed Method implemented by Person2" );
}
public void Distance( int p2d) {
di2 = p2d;
Console.WriteLine( "Distance Method implemented by Person2" );
}
public void display2() {
Console.WriteLine( "The Speed of 2nd person is: " +sp2);
Console.WriteLine( "The distance covered by 2nd person is: " +di2);
}
}
public class GFG {
public static void Main(String []args) {
Person1 obj1 = new Person1();
Person2 obj2 = new Person2();
Race r;
r = obj1;
r.Speed(10);
r.Distance(50);
obj1.display1();
r = obj2;
r.Speed(15);
r.Distance(45);
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:
using System;
interface Vehicle {
void changeGear( int a);
void speedUp( int a);
void applyBrakes( int a);
void printStates();
}
class Bicycle : Vehicle {
int speed;
int gear;
public void changeGear( int newGear)
{
gear = newGear;
}
public void speedUp( int increment)
{
speed = speed + increment;
}
public void applyBrakes( int decrement)
{
speed = speed - decrement;
}
public void printStates()
{
Console.WriteLine( "speed: " + speed + " gear: " + gear);
}
}
class GFG {
public static void Main(String[] args)
{
Bicycle bicycle = new Bicycle();
Vehicle obj;
obj = bicycle;
obj.changeGear(4);
obj.speedUp(5);
obj.applyBrakes(2);
Console.WriteLine( "Bicycle Present State:" );
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...