Where did the concept of Interfaces come from?

后端 未结 18 2103
有刺的猬
有刺的猬 2021-01-11 18:45

In c#, we have interfaces. Where did these come from? They didn\'t exist in c++.

相关标签:
18条回答
  • 2021-01-11 19:31

    They came from java, and they were introduced because java (and C#) do not allow multiple inheritance.

    EDIT: I'm receiving some downmods because people using COM interfaces in C++ disagree with the above statement. Regardless, the concept of an interface came from java, C++ COM interfaces were virtual classes, java was the first language to make it a language feature.

    END EDIT

    For example, in C++, you could have a class named Dog that inherited from Animal and Mammal.

    In C#, you would have a base class named Animal, and use an interface (IMammal). The I naming notation is historical from C++ (It was used to indicate an abstract virtual class), and was carried over to java but is more significant in C#, because there is no easy way to tell what is a base class and what is a interface from a C# class declaration:

    public class Dog : Animal, IMammal
    

    while in Java it was more obvious:

    public class Dog extends Animal implements IMammal
    

    Multiple inheritance is very tricky, so interfaces were derived to simplify it. A C# class can only inherit from one base class, but can implement N amount of interfaces.

    In C++, interfaces can be simulated by using pure virtual classes. These require all methods to be overridden polymorphicaly by the inheriting class.

    0 讨论(0)
  • 2021-01-11 19:31

    In C++ you could have an abstract class with no implementation, and you could inherit multiple classes. Java and C# got rid of multiple inheritance, so in order to have the ability to inherit multiple contracts (not behaviors), they created interfaces. You can only inherit one class in C#, but you can inherit as many interfaces as you want.

    An interface is jst a contract. It says which members that an instance must implement. It does this, however, without implementing any default behaviors.

    0 讨论(0)
  • 2021-01-11 19:31

    While not called 'interfaces', C data structure pointers with function pointers as elements of the structure implemented the concept of interfaces long before c++ did with virtual base classes IMO.

    0 讨论(0)
  • 2021-01-11 19:34

    Interfaces came from computer science. Or, let's say, from common sense in programming. Interface is a logical group of methods of a class. C++ didn't need a separate language concept of "interface", because any class might be used as an interface -- just define a set of methods in it, make no implementation, call it like IExecutable and use:

    class IExecutable
    {
    public:
        virtual void Execute() = 0;
    };
    
    class MyClass : public IExecutable
    {
    public:
        void Execute() { return; };
    };
    

    Some languages, called "dynamically typed", like Python, don't require to define interfaces at all, you just call a method you need, and run-time checks if it is possible ("If it walks like a duck and talks like a duck, it must be a duck").

    C# clearly separates a concept of interfaces from classes, because it uses static typing... and multiple inheritance is prohibited in that language, but it is ok for a class to have one base class and another interface, or to implement several interfaces at a time.

    public interface IPurring
    {
        void Purr();
    }
    
    public class Cat : Animal, IPurring
    {
        public Cat(bool _isAlive)
        {
            isAlive = _isAlive;
        }
    
        #region IPurring Members
    
        public void Purr()
        {
            //implement purring
        }
    
        #endregion
    }
    
    0 讨论(0)
  • 2021-01-11 19:35

    Interfaces existed in C++ if you did COM programming, which is where the IPrefix convention originates.

    Although C++ itself didn't natively support interfaces, COM/C++ used type libraries generated from Interface Definition Language which has the only purpose of defining interfaces, and used the interface keyword long before Java or C# did.

    Aside from allowing a form of multiple inheritence, .NET's motivation for interfaces have to do with its component-oriented origins and its main purpose is to define contracts between components that can interoperate without any knowledge of each other's implementations. Some COM interop is also done with .NET interfaces.

    0 讨论(0)
  • 2021-01-11 19:35

    I trink interfaces came from the fact that some programmers got tired of writing the implementation of a method over and over again. How many times can you write:

    static string Method(int i)
    

    without thinking there has to be an easier way?

    0 讨论(0)
提交回复
热议问题