Interface vs Abstract Classes

大城市里の小女人 提交于 2019-12-19 09:24:44

问题


I am a little bit familiar with the difference between Abstract and Interface classes but What do you think is the meaning of the sentence below?

An interface can only define constants while abstract class can have fields.


回答1:


An interface can only define constants while abstract class can have fields.

your field from interface is by implicitly public, static, final

which isn't case with abstract class




回答2:


constants - static, not changing (static final)
fields - instance-specific, changing

Since interfaces cannot be instantiated, you can only have static and not-changing properties. On the other hand abstract classes can be be extended, and their subclasses - instantiated, so you can have instance-specific, changing properties.




回答3:


Well the statment is technically incorrect what they are refering to is that all variables on an interface must be declared static whereas abstract classes have no such limitation.

The statement is incorrect since Java does not have constants only final which are still modifiable and thus not constant.




回答4:


Additional to Jigar Joshi answer. We can implements any number of interface but we can extend only one abstract class.



来源:https://stackoverflow.com/questions/5953851/interface-vs-abstract-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!