0

Possible Duplicate:
When to use abstract class or interface?

I am a newbie in Java , can anyone please explain a scenario where abstract class will be useful and interface will not be and vice versa. I believe in not so complex problems both can solve the issue with equal ease. Please explain in layman's term and pardon my ignorance!

Community
  • 1
  • 1
user1138406
  • 71
  • 1
  • 4

1 Answers1

2

When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

Abhishek bhutra
  • 1,400
  • 1
  • 11
  • 29
  • I am struck with following logic-"Why to use abstract class at all , instead use interface every time and thus sustain the class's ability to extend another class which gets compromised in case of using abstract class . " any real life design example will be great use !! – user1138406 Jan 09 '12 at 10:20