Interface versus abstract classes

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

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

+3
source share
4 answers

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

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

which does not belong to an abstract class

+6
source

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

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

+6
source

It is good that the statistical statement is technically incorrect, what they refer to is that all variables on the interface must be declared static, while abstract classes do not have such a restriction.

The statement is wrong, because Java does not have only final constants, which are still mutable and therefore not constants.

+1
source

Additional answer by Jigar Joshi. We can implement any number of interfaces, but we can extend only one abstract class.

0
source

Source: https://habr.com/ru/post/891210/


All Articles