What is the difference between an interface and ab

2018-12-31 02:16发布

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

30条回答
素衣白纱
2楼-- · 2018-12-31 02:35

enter image description here

Here is a very basic understanding over interface vs abstract class.

查看更多
余欢
3楼-- · 2018-12-31 02:36

By definition, interfaces cannot have an implementation for any methods, and member variables cannot be initialized.

However, abstract classes can have methods implementated and member variables initialized.

Use abstract classes when you expect changes in your contract, i.e., say in future you might need to add a new method.

In this situation, if you decide to use an interface, when the interface is changed to include interface, your application will break when you dumped the new interface dll.

To read in detail, visit difference between abstract class and a interface

查看更多
临风纵饮
4楼-- · 2018-12-31 02:39

usually Abstract class used for core of something but interface used for appending peripheral.

when you want to create base type for vehicle you should use abstract class but if you want to add some functionality or property that is not part of base concept of vehicle you should use interface,for example you want to add "ToJSON()" function.

interface has wide range of abstraction rather than abstract class. you can see this in passing arguments.look this example:

enter image description here

if you use vehicle as argument you just can use one of its derived type (bus or car-same category-just vehicle category). but when you use IMoveable interface as argument you have more choices.

查看更多
十年一品温如言
5楼-- · 2018-12-31 02:42

Not really the answer to the original question, but once you have the answer to the difference between them, you will enter the when-to-use-each dilemma: When to use interfaces or abstract classes? When to use both?

I've limited knowledge of OOP, but seeing interfaces as an equivalent of an adjective in grammar has worked for me until now (correct me if this method is bogus!). For example, interface names are like attributes or capabilities you can give to a class, and a class can have many of them: ISerializable, ICountable, IList, ICacheable, IHappy, ...

查看更多
萌妹纸的霸气范
6楼-- · 2018-12-31 02:43

I'd like to add one more difference which makes sense. For example, you have a framework with thousands of lines of code. Now if you want to add a new feature throughout the code using a method enhanceUI(), then it's better to add that method in abstract class rather in interface. Because, if you add this method in an interface then you should implement it in all the implemented class but it's not the case if you add the method in abstract class.

查看更多
长期被迫恋爱
7楼-- · 2018-12-31 02:44

The only difference is that one can participate in multiple inheritance and other cannot.

The definition of an interface has changed over time. Do you think an interface just has method declarations only and are just contracts? What about static final variables and what about default definitions after Java 8?

Interfaces were introduced to Java because of the diamond problem with multiple inheritance and that's what they actually intend to do.

Interfaces are the constructs that were created to get away with the multiple inheritance problem and can have abstract methods, default definitions and static final variables.

See Why does Java allow static final variables in interfaces when they are only intended to be contracts?.

查看更多
登录 后发表回答