Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface and abstract class

 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface and abstract class contains method signature,then what are the difference between both,I knew interface are useful for multiple inheritance.Where interface .abstract class would be used ?
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please do a search of the Ranch; this has come up several times on this forum and beginners' in the last two weeks. You might find the old threads answer your question.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15274
38
Android IntelliJ IDE Java Scala Spring
 
James Basller
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jacob deiter:
Interface and abstract class contains method signature,then what are the difference between both,I knew interface are useful for multiple inheritance.Where interface .abstract class would be used ?


Differences between both interfaces and abstract classes are

1. Interfaces are used for multiple inheritance while as per Java we can not extend more than one class.

2. Interfaces are only specifies variables and method signatures where Abstract classes can have variables and also methods with only signatures (Abstract Methods) and also methods with the implementation.

Similarities between these 2 is these both can not be instantiated.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also whilst interfaces are 100% abstract, Abstract classes can have non-abstract methods, including a constructor one could say that there is a degree of abstraction flexibility between an abstract class and a interface.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would go farther than that; I think there is a spectrum from abstract to concrete

Interface (very abstract)
Abstract class
Ordinary (Concrete) class
Final class
Enum member (read-only: very concrete).

Members of an enum are very concrete; they cannot be changed and must be regarded as read-only. You could add "immutable class" and "abstract class with no abstract methods" to that spectrum if you wish.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic