Why do we use Interface? Is it only for Standardiz

2019-01-03 01:19发布

Why do we use Interface?

Is it only for Standardization?

13条回答
成全新的幸福
2楼-- · 2019-01-03 01:30

Interface provide prototype modal that just contains declaration of functionality of a specific behavior.

and if u want to implement this behavior into class then u must implement this interface in class then class have this behavior functionality or it can have multiple behavior.

because class can implement multiple interface.

查看更多
叼着烟拽天下
3楼-- · 2019-01-03 01:32

The main reason the interfaces are used in languages like C#/Java is because those languages don't support multiple (class) inheritance (see What is the exact problem with multiple inheritance?).

But multiple (interface) implementation is permited allowing classes to be used in diferent ways.

查看更多
成全新的幸福
4楼-- · 2019-01-03 01:32

Interface separates the data type from the implementation logic.

查看更多
Viruses.
5楼-- · 2019-01-03 01:34

An interface is used to describe what an implemented thing can do. So you have the possibility to treat several objects which implementing the same interface as a type of this interface.

For example:

public interface IMyInterface{
    public void DoFirst();
    public int DoSecond();
}


public class A : IMyInterface{
   //class has to implement DoFirst and DoSecond
   public void DoFirst(){
     Console.WriteLine("Blubb1");  
   }

   public int DoSecond(){
     Console.WriteLine("Blubb2");
     return 2;  
   }
}

public class B : IMyInterface{
   //class has to implement DoFirst and DoSecond
   public void DoFirst(){
     Console.WriteLine("Blibb1");  
   }

   public int DoSecond(){
     Console.WriteLine("Blibb2");  
     return 4;
   }
}

The classes implement the Interface in several ways. But you can use them as IMyInterface. For example:

public static void DoMethodsInInterface(IMyInterface inter){
    inter.DoFirst();
    inter.DoSecond();
}


public static void main(){

   DoMethodsInInterface(new A());
   DoMethodsInInterface(new B());
   //Or use it in a List
   List<IMyInterface> interlist = new List<IMyInterface>();
   interlist.Add(new A());
   interlist.Add(new B());
   foreach(IMyInterface inter in interlist){
      inter.DoFirst();
   }

}

I hope this makes a bit clear why interfaces are useful.

查看更多
一纸荒年 Trace。
6楼-- · 2019-01-03 01:43

Following are the main reasons for the use of interfaces

  1. Extensibility
  2. Implementation Hiding
  3. Accessing object through interfaces
  4. Loose Coupling.

please visit this link to know about interfaces with downloadable code example

查看更多
淡お忘
7楼-- · 2019-01-03 01:46

Here's the high level view...

Interfaces play a big role in the concept of Information Hiding.

They basically help you hide the implementation details of your class so that a calling class does has no dependency on that implementation. Therefore, by using interfaces you can modify the implementation without changing the calling class. This all in turns limits the complexity of your code and make it easier to maintain in the long run.

When I first started understanding interfaces they were explained to me as a "contract that provides a description your class." Not sure if that will help you but if you think of an interface for a car you could say that it drives, breaks, and turns. So as long as it gets me from point A to point B, I don't really have to know how those functions are implemented.

查看更多
登录 后发表回答