When to implement an interface and when to extend a superclass?

后端 未结 11 867
遇见更好的自我
遇见更好的自我 2020-11-30 04:41

I\'ve been reading a lot about interfaces and class inheritance in Java, and I know how to do both and I think I have a good feel for both. But it seems that nobody ever rea

11条回答
  •  误落风尘
    2020-11-30 05:03

    I guess I'll give the classic car example.

    When you have a car interface, you can create a Ford, a Chevy, and an Oldsmobile. In other words, you create different kinds of cars from a car interface.

    When you have a car class, you can then extend the car class to make a truck, or a bus. In other words, you add new attributes to the sub classes while keeping the attributes of the base or super class.

提交回复
热议问题