• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract classes and interfaces  RSS feed

 
Abigail Decan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was confused between the two, so i did research.
again, i would appreciate it if someone could confirm.


interface:
methods - abstract, default, static ONLY
(abstract methods have no body, while static and defaults do, right?)
fields - public, static, final ONLY

abstract class: a normal class, but has at least one abstract method
methods - all
ie static, non-static, abstract (can it have a default method?)
fields - all
ie public, protected, private / final, non-final / static, non-static

thank you!
 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abigail, I hope I'm not side-stepping your question, but the best way to think about the difference (abstract classes versus interfaces) is to understand the reasons why you'd use one over the other. (If you understand the reasons, then the rules feel more natural to you.) I remember several good online articles about the difference. The one that I found just now is at http://www.programmerinterview.com/index.php/java-questions/interface-vs-abstract-class.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abigail Decan wrote: . . .
interface:
methods - abstract, default, static ONLY
. . .
nonononononononono.

Interface methods are never static. Interface methods with default implementations only exist in Java8.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abigail Decan wrote:interface:
methods - abstract, default, static ONLY

No. Interface methods are abstract and public.

fields - public, static, final ONLY

Correct. However, defining fields in an interface is not generally a good idea.

abstract class: a normal class, but has at least one abstract method

No. An abstract class does not have to have abstract methods; however, a class must be abstract if it declares an abstract method. Do you see the distinction?

methods - all

Correct.

fields - all

Correct.

Have a look at Barry's link because he's absolutely right: it's better to know why you would choose one over the other, because some of their characteristics overlap.

I'd just add a couple of things to it:
1. Inheritance is not needed as often as you might think, and large hierarchies of classes tend to be frowned on these days.
2. One common use for abstract classes is as partial or 'skeleton' implementations for an interface. The Java Collections Framework has many examples of this: CollectionAbstractColletion, ListAbstractList, MapAbstractMap, and several more - and they're very useful.

HIH

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!