• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic