• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

abstract class vs interface ?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when should i use abstract class rather than interface ? and when use interface rather than abstract class ?
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See http://faq.javaranch.com/view?InterfaceVsAbstractClass
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic