3

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.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
newbie
  • 14,582
  • 31
  • 104
  • 146
  • 2
    Can you tell us what YOU think it is? – entonio May 10 '11 at 17:20
  • Also, when you searched here for related question, what part of the related answers did you find confusing? Lots of these questions seem relevant: http://stackoverflow.com/search?q=interface+abstract+class – S.Lott May 10 '11 at 17:21
  • http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class answers your questions – Ravindra babu Sep 25 '16 at 16:50

4 Answers4

6

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

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Now I understand what it means... that I can create ONLY an attribute that is constant in the Interface. – newbie May 10 '11 at 17:28
6

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

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.

Sled
  • 18,541
  • 27
  • 119
  • 168
0

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

Farid Movsumov
  • 12,350
  • 8
  • 71
  • 97