Interface vs Abstract Classes

2019-02-18 12:42发布

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.

4条回答
Summer. ? 凉城
2楼-- · 2019-02-18 13:32

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

查看更多
来,给爷笑一个
3楼-- · 2019-02-18 13:33

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楼-- · 2019-02-18 13:34

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

查看更多
乱世女痞
5楼-- · 2019-02-18 13:36

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.

查看更多
登录 后发表回答