I have gone through many examples for Abstract classes and Interface but I am not getting wats the use of doing so, because both are doing same job .Can anybody give me easy example an figure out this riddle
-
1They do *not* do the same job. And there *definitely* is a use, otherwise they probably wouldn't be in the language. See [this](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) question for why interfaces are useful, and see [this](https://programmers.stackexchange.com/questions/96947/why-should-i-declare-a-class-as-an-abstract-class) for some reasons for abstract classes to be useful. If you think they are doing the same job you do not yet understand them. I would close this as a dupe, but no single question contains the answer to this one... – awksp Jun 27 '14 at 06:25
-
http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class – Kamlesh Arya Jun 27 '14 at 06:31
3 Answers
First of all,revision of BASIC DIFFFERENCES :-
Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
A Java class can implement multiple interfaces but it can extend only one abstract class. Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
Consider using abstract classes if any of these statements apply to your situation:
You want to share code among several closely related classes. You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to your situation:
You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
You want to take advantage of multiple inheritance of type.
(Taken from Oracle's Official Tutorials).

- 18,735
- 7
- 49
- 73
-
2First point is no longer true as of Java 8. Also, according to [this](https://stackoverflow.com/questions/6839943/why-are-interface-method-invocations-slower-than-concrete-invocations) question, the last point isn't true any more. – awksp Jun 27 '14 at 06:31
-
Ok,Thanks. I'll edit it as per your saying.Actually,I ain't sure as I haven't read the Lambda Functions and default methods and so! THANKS. – Am_I_Helpful Jun 27 '14 at 06:32
-
Sorry, should have been more specific. Java interfaces don't have implementations by default, but since Java 8 you can implement methods using the `default` keyword. Also, take a look at the question I linked. Seems like point 6 isn't true any more. – awksp Jun 27 '14 at 06:34
-
@user3580294-Oh sorry,I posted it at wrong answer.Really,this wasn't my intention on this answer! – Am_I_Helpful Jul 16 '14 at 17:50
One simple answer about when to use interface and abstract class is: Use Interface when you know that you won't have to modify your interface, that is add/delete/modify methods because if you change the implementation of interface, you will have to do the same in all the classes where this particular interface is being implemented.
On the other side, Abstract classes should be used where you know that over the time there is a possibility to add/delete/modify the method. Hence if you have to add a method, you can add it in the abstract class without declaring it as abstract method.
I read this somewhere but can't find the reference now...
Hope this helps..

- 1,511
- 2
- 17
- 37
Once i stumbled upon a very good article on this which describes :
Feature
Multiple inheritance
Difference
A class may inherit several interfaces.
A class may inherit only one abstract class.
Feature
Default implementation
Difference
An interface cannot provide any code, just the signature.
An abstract class can provide complete, default code and/or just the details that have to be overridden.
Feature
Access Modfiers
Difference
An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public
An abstract class can contain access modifiers for the subs, functions, properties
Feature
Core VS Peripheral
Difference
Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
An abstract class defines the core identity of a class and there it is used for objects of the same type.
Feature
Homogeneity
Difference
If various implementations only share method signatures then it is better to use Interfaces.
If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Feature
Speed
Difference
Interfaces requires more time to find the actual method in the corresponding classes.
While abstract classes does it fast
Feature
Adding functionality (Versioning)
Difference
If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Feature
Fields and Constants
Difference
No fields can be defined in interfaces
An abstract class can have fields and constrants defined
You can refer this for more information

- 8,806
- 4
- 29
- 34
-
As I commented on Shekar's post, interfaces can have implementations as of Java 8, and it seems that there isn't a speed difference between the two any more. Also, you *can* define fields in interfaces. – awksp Jun 27 '14 at 06:45