-1

Possible Duplicate:
When to use an interface instead of an abstract class and vice versa?

Can someone give me an idea of when the abstract class and interfaces has to be used?

IF possible supporting with a real time example?

Thanks in Advance.

Community
  • 1
  • 1
user254582
  • 121
  • 1
  • 3
  • 12
  • The short answers is if you can use an interface, use that (its simpler) Its one of the most common questions about Java. Google will find you thousands of examples. "Real time" has a specific meaning, I assume you mean "real world" – Peter Lawrey Sep 06 '11 at 06:38
  • http://stackoverflow.com/questions/1221512/abstract-class-and-interface-class – jmj Sep 06 '11 at 06:39
  • http://stackoverflow.com/questions/56867/interface-vs-base-class – Stephen C Sep 06 '11 at 06:41
  • The really short answer is to search for existing answers before you post (yet another duplicate) question. If I/we can find them, so can you. – Stephen C Sep 06 '11 at 06:43

2 Answers2

0

An interface is a complete abstraction of some role. You use an interface when you want to ensure certain related behaviours in a type.

An abstract class is more of a default behaviour / infrastructure provider for a collection of classes with some commonality. You use an abstract class when you have some idea of how a collection of classes should behave but would like to refine and augment the behaviour specifically in the individual classes that are extending the abstract class.

0

abstract classes are designed with implementation gaps for sub-class to fill in.

interfaces are syntactically similar to classes but they lack instance variables & methods.

abstract classes can also have both abstract methods & non-abstract methods. where as in interface methods are abstract only, & variables are implicitly static&final.

see the links for more detail

http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/

http://www.sap-img.com/java/when-we-go-for-abstract-and-interface.htm

Pratik
  • 30,639
  • 18
  • 84
  • 159