• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class vs interface ?

 
sam mon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when should i use abstract class rather than interface ? and when use interface rather than abstract class ?
 
Andris Jekabsons
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use interface when you don't need to provide ANY implementation.

Use abstract class when you need to provide SOME implementation, but leave the rest unimplementated.

The way I picture it is that interface just gives you most basic direction what your class should do. Abstract class implementats those directions, but doesn't do it fully - just becomes a "half-done" class (and there are situations when you'd need that).
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam,

* Use abtract classes for fundamentals concepts in your OO design. Implement abstract classes with basic and and common services of a class hierarchy.

* Use Interface as "requirements class" for a process. Interfaces enable to describe what you will find at least in a class. For example, when a method need and Interface X instance to run, you don't care about what class instance you actually use, you only care about the fact that the instance you use meet the minimum requirements described in the interface X.

Hope that could be useful,
best regards,
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See http://faq.javaranch.com/view?InterfaceVsAbstractClass
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic