Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when to use abstract class & when interface

 
felix thomas
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

could any one with real life explain me when we should use abstract class and when we should use interface.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Felix,
You use an interface when you are defining a contract for the class. For example, "all Display objects will implement print()."

You use an abstract class when you want to provide some base functionality. You also use an abstract class when you want to provide a template and the caller just has to fill in a few methods that get called in the middle.
 
Michael Herrmann
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use abstract classes that don't contain a single non-abstract method as well because some classes will always be only one thing and are never going to be another at the same time. I use abstract classes in this case in order to minimize the sources of bugs and bad design (classes that do too much).
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
once you understand the different concepts ruling abstract classes and interfaces you'll know when to use which...

An interface defines a contract the implementing class promises to abide by.
An abstract class tells you that the extending class IS something.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic