Interface interacts between classes

Can we say that an interface interacts between two classes? How can he send information from class B to class C? both classes inherit the same interface.

In this example, I read

lets take your "hand" as an example. "Hand" is a class. You have two hand-type objects in your body called the left hand and the right hand. Their main functions are or are controlled by a set of electrical signals transmitted through your shoulders (via an interface). Thus, the shoulder is the interface your body uses to communicate with your hands. The hand is a well thought out class. The arm is reused to create the left arm and the right arm, slightly altering its properties.

it only indicates that the interface is controlling or manipulating a class, I agree with this point, but somehow I found out that an interface can transfer data from one class to another, so it is correct if we define an interface like this or what to do we use the interface

An interface creates a link between two classes, for example Iabc interface inherited in ClassA and ClassB, then it can send ClassA information to ClassB.

public interface  Interface1
{
    void  Method1(string msg);
     void Method2(string msg1 ,string msg2);
}
 public static class  HelperClass
 {
     public static void Method1(Interface1 obj ,string msg)
     {
         obj.Method1(msg);
     }

     public static void Method2(Interface1 obj,string msg1, string msg2)
     {
         obj.Method2(msg1,msg2);
     }
 }
  static void Main(string[] args)
    {
        var Car = new Vehcile();
        var HS = new Person();
        Car.per= "Car Clss";
        HS.per = "HS Clss";
        HelperClass.Method1(Car, Car.per);
        HelperClass.Method1(HS, HS.per);
        HelperClass.Method2(Car, Car.per, HS.per);
        HelperClass.Method2(HS, HS.per, Car.per);
        Console.ReadKey();
    }

     public class Person : Interface1
 {

    public String per;

     void Interface1.Method1(string msg)
    {
        Console.WriteLine(msg);
    }

    void Interface1.Method2(string msg1, string msg2)
    {
        Console.WriteLine("Person Class" + msg1 + " " + msg2);
    }
}

 class Vehcile : Interface1
{
    public String per;

     void Interface1.Method1(string msg)
    {
        Console.WriteLine(msg);
    }

    void Interface1.Method2(string msg1, string msg2)
    {
        Console.WriteLine("Vehcile Class" + msg1 + " " + msg2);
    }
}

      

+3


source to share


1 answer


Would you say that an interface interacts between two classes?

I wouldn't define an interface as such. I would look at the interface as a binding contract. The contract says, "Anyone who implements this interface must be able to perform whatever actions are specified in the contract."

For example:

public interface IHand
{
    void Shake(IHand otherHand);
}

public class Hand : IHand
{
    public void Shake(IHand otherHand)
    {
        Console.WriteLine("Shook the other hand");
    }
}

      



IHand

is an interface, and it declares a named method Shake

that gets another object IHand

. Any class that implements our interface must provide a method Shake

that does something.

In this particular example, our class Hand

writes to the console every time it shakes its other hand.

Through interfaces, we can create abstractions . The meaning, instead of depending on a specific class (for example Hand

), we can only depend on the contract . This means that any object that implements is IHand

fine with us, because it is guaranteed that it will have a method Shake

that we can call. What happens inside the method Shake

is outside of us, and we usually don't care.

+5


source







All Articles