What is the difference between an interface and abstract class?

前端 未结 30 1819
情歌与酒
情歌与酒 2020-11-21 11:51

What exactly is the difference between an interface and abstract class?

30条回答
  •  粉色の甜心
    2020-11-21 12:15

    I am 10 yrs late to the party but would like to attempt any way. Wrote a post about the same on medium few days back. Thought of posting it here.

    tl;dr; When you see “Is A” relationship use inheritance/abstract class. when you see “has a” relationship create member variables. When you see “relies on external provider” implement (not inherit) an interface.

    Interview Question: What is the difference between an interface and an abstract class? And how do you decide when to use what? I mostly get one or all of the below answers: Answer 1: You cannot create an object of abstract class and interfaces.

    ZK (That’s my initials): You cannot create an object of either. So this is not a difference. This is a similarity between an interface and an abstract class. Counter Question: Why can’t you create an object of abstract class or interface?

    Answer 2: Abstract classes can have a function body as partial/default implementation.

    ZK: Counter Question: So if I change it to a pure abstract class, marking all the virtual functions as abstract and provide no default implementation for any virtual function. Would that make abstract classes and interfaces the same? And could they be used interchangeably after that?

    Answer 3: Interfaces allow multi-inheritance and abstract classes don’t.

    ZK: Counter Question: Do you really inherit from an interface? or do you just implement an interface and, inherit from an abstract class? What’s the difference between implementing and inheriting? These counter questions throw candidates off and make most scratch their heads or just pass to the next question. That makes me think people need help with these basic building blocks of Object-Oriented Programming. The answer to the original question and all the counter questions is found in the English language and the UML. You must know at least below to understand these two constructs better.

    Common Noun: A common noun is a name given “in common” to things of the same class or kind. For e.g. fruits, animals, city, car etc.

    Proper Noun: A proper noun is the name of an object, place or thing. Apple, Cat, New York, Honda Accord etc.

    Car is a Common Noun. And Honda Accord is a Proper Noun, and probably a Composit Proper noun, a proper noun made using two nouns.

    Coming to the UML Part. You should be familiar with below relationships:

    • Is A
    • Has A
    • Uses

    Let’s consider the below two sentences. - HondaAccord Is A Car? - HondaAccord Has A Car?

    Which one sounds correct? Plain English and comprehension. HondaAccord and Cars share an “Is A” relationship. Honda accord doesn’t have a car in it. It “is a” car. Honda Accord “has a” music player in it.

    When two entities share the “Is A” relationship it’s a better candidate for inheritance. And Has a relationship is a better candidate for creating member variables. With this established our code looks like this:

    abstract class Car
    {
       string color;
       int speed;
    }
    class HondaAccord : Car
    {
       MusicPlayer musicPlayer;
    }
    

    Now Honda doesn't manufacture music players. Or at least it’s not their main business.

    So they reach out to other companies and sign a contract. If you receive power here and the output signal on these two wires it’ll play just fine on these speakers.

    This makes Music Player a perfect candidate for an interface. You don’t care who provides support for it as long as the connections work just fine.

    You can replace the MusicPlayer of LG with Sony or the other way. And it won’t change a thing in Honda Accord.

    Why can’t you create an object of abstract classes?

    Because you can’t walk into a showroom and say give me a car. You’ll have to provide a proper noun. What car? Probably a honda accord. And that’s when a sales agent could get you something.

    Why can’t you create an object of an interface? Because you can’t walk into a showroom and say give me a contract of music player. It won’t help. Interfaces sit between consumers and providers just to facilitate an agreement. What will you do with a copy of the agreement? It won’t play music.

    Why do interfaces allow multiple inheritance?

    Interfaces are not inherited. Interfaces are implemented. The interface is a candidate for interaction with the external world. Honda Accord has an interface for refueling. It has interfaces for inflating tires. And the same hose that is used to inflate a football. So the new code will look like below:

    abstract class Car
    {
        string color;
        int speed;
    }
    class HondaAccord : Car, IInflateAir, IRefueling
    {
        MusicPlayer musicPlayer;
    }
    

    And the English will read like this “Honda Accord is a Car that supports inflating tire and refueling”.

提交回复
热议问题