C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can allude to any protest that executes its interface. An interface reference variable fair knows that functions/methods which are declared by its interface affirmation. It does not permit getting to any other factors or functions/methods that could be backed by the objects. This concept is comparable once you utilize a parent course reference to get to a child course protest. Underneath are the illustrations to demonstrate the concept of Interface.

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 CodeConfig
 {
    public static void Main(String []args) {

        Person1 obj1 = new Person1();
        Person2 obj2 = new Person2();
        Race r;
        // ----- For Person1 Class ----------
        // assigning Person1 object 'obj1' to interface Reference 'r'
      
        r = obj1;
         
        // Now you can access the abstract functions/method
        // of Race interface which are implemented  by class Person1
      
        r.Speed(10);
        r.Distance(50);
 
        // if you will try to call display1() 
        // functions/method using 'r' it will give error
        //r.display1();
         
        // calling the display1() 
        // functions/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 functions/method
        // of Race interface which are implemented
        // by class Person2
        r.Speed(15);
        r.Distance(45);
 
        // if you will try to call display2() 
        // functions/method using 'r' it will give error
        //r.display2();
         
        // calling the display1() 
        // functions/method of Person1 Class
        obj2.display2();
         
    }
     
}

Output:

Speed Method implemented by Person1.
Distance Method implement 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 of above Example:

In above example, we have an interface named Race a two classes Person1 and Person2 which are implementing the functions/methods of the interface. The Person1 class has its own functions/method named display1() and similar Person2 class its own functions/method display2() which cannot be called by using interface reference. In order to call the functions/methods using interface reference (here r is interface reference), you have to assign to class object to it. similar as if you are assigning Person1’s object obj1 to r i.e. r = obj1; then you call the Speed() and Distance() functions/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 functions/methods of Person2 class.

Let’s Check one more example below:

Example 2:

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;

    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);
    }
}
 
// Driver Class
class CodeConfig {
 
    // 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 functions/method of class Bicycle 
        obj.printStates();
    }
}

Output:

Bicycle    Present    State:
speed:    3    gear:    4

Explanation of above Example:

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 have used to refer the object bicycle of the Bicycle class.

We hope above post has done some valuable addons to your knowledge bank. Feel free to share your feedback in comments section. This will help us to improve the quality of our posts.

Leave a Reply

Your email address will not be published. Required fields are marked *