-1

Possible Duplicate:
When to use abstract class or interface?

I know about abstract classes and interfaces. But I wish to know a real time scenario in which we need to use only abstract class but not interface and only interface but not an abstract class.. Can any one suggest me some example like that.

I guess the second one is used when multiple inheritance need arises i.e. A class is already inheriting some class and so we cant use an abstract class here .. We need to use only interface. In the same way I need an example In which we use only abstract but not interface.

EDITED

Just to add some more points for interface.

Consider the java library collections

                          Collections

            List                                Set

All 3 are interfaces but the major thing , though they have functions which are common that is add , addAll, contains,containsAll etc. List implements in its own and set implements in another way ( telling about duplication ). Set does not allow duplicates list allows duplicates.

So what I am trying to say is when ever there is no common features for sub-interface of super-interface we have to go for interface

But if there is some common functionality which both has then keep it as to be abstract class.

I need a practical example done in java api. Thanks. Sindhu

Community
  • 1
  • 1
sai sindhu
  • 1,155
  • 5
  • 20
  • 30
  • 3
    There are literally *thousands* of answers to this question floating around the internet. – dlev May 17 '12 at 19:33
  • Sindhu, is this homework? Please share your best guess at an answer, and we can tell you if you're on the right track. – rajah9 May 17 '12 at 19:34

2 Answers2

0

Abstract classes are good for common functionality, which doesn't have much to do with the interface which is defining the API.

You could skip the interface if you'll never have an implementation other than a subclass of the abstract class. I can't really think of a practical example though since I'd always want the interface so I could create mock implementations for unit testing.

Skipping the abstract class is much more common. In that case each item honors an interface, but shares no implementation. You might have a "ConnectionService" interface with implementations for JDBC, FTP, etc, where those implementation are so specific to their protocol that an abstract class adds no value.

Chris Kessel
  • 5,583
  • 4
  • 36
  • 55
0

Usually, you use a Super Class hierarchy when you want your classes to be able to inherit from the previous class in the chain.

A Super Class could be Animal

Animal can be herbivore, or carnivore.

herbivore can be a horse.

carnivore can be a wolf.

horse can be a faun.

wolf can be a cub.

And so on.

You usually only implement interfaces when you want to force certain functionality to be added, but different on a case-by-case basis, since an Interface only implements method signatures but not actual functionality. You have to write that.

OmniOwl
  • 5,477
  • 17
  • 67
  • 116