接口与摘要差异的理论解释[关闭]

I need theoritical explanation on the difference between Interface and Abstract class with an example.

Please donot reply any code as I already know that. Am asking in design level.

For Eg: I need to design an application for Automobile industry where they manufacture Car and Bike.

So here AutomobileIndustry is parent class and Car, Bike are Inherited from it.

I need to know what functionalities should be put into Interface and what should be in Abstract class.

In general I like to think of a base class (abstract or otherwise) as what something is, while an interface is what something does. So something is a Vehicle or an Automobile or some such, while what it does is be Drivable or Flyable, etc.

The base class is a common structure of existence, while the interface is a common contract of expected functionality.

You shouldn't put any functionality at all into an interface; an interface is nothing more than a definition of what should be defined in any class that implements it. As an example: your classes such as bike, car, truck, etc should all have methods for moveForward(), moveBackward(), turnRight(), turnLeft() and stop(); but the implementation of these is going to be different for each vehicle type... you define an interface of vehicle that specifies that all these methods must be defined, then each class you create for a specific vehicle type must implement all of these methods.

When typehinting, you can specify vehicle as the typehint; then any traffic class methods that accepts an argument of type vehicle know that the class they accept will have those methods available for it to call.

An abstract contains common methods and properties that are available to all child classes that inherit from it, so you don't need to re-define those identical blocks of code in every child class, but use inheritance so that they are only defined in a single place. Note that an abstract cannot be instantiated, so it can only ever be used as a parent for other class definitions.

All methods in interface should be implemented by classes who implement it. An interface contains actions that implementing classes should perform.

Methods in an abstract class are available to all derived class. You should use an abstract class if there is a generic class from which several classes could derive. For instance, an Animal class can be abstract, with abstract methods that all derived classes - Tiger, Bear, Dolphin - can choose to implement or not.

This can be an interesting start.

It is also worth adding that a class can implement multiple interfaces but it can only extend one class (abstract or concrete).

Another difference is that an abstract class can have instance variables and methods with a body whereas interface cannot (interface can only have methods without a body).

Think of an abstract class as a very generic kind of something that shouldn't be instantiated because it simply wouldn't make much sense to do so (e.g. abstract class animal or shape, both of which are very generic and should be only extended and not instantiated (I know this could vary based on the design but I'm just trying to make a point)).

An interface is simply something that guarantees that a class that implemented it has implemented all the methods that were defined in the interface.