Conceptually I know the difference between abstract class and interface. But wondering about the technical difference between these two. Why Sun made this interface term even though I can have fully abstract class and make my work done.
-
2It's how Sun avoided all the problems of multiple inheritance. A class can be a direct subtype of multiple interfaces, but of only one abstract class; so you don't get the diamond problem (at least until Java 8, which introduced the diamond problem to Java for the first time). – Dawood ibn Kareem May 15 '14 at 05:08
-
you might wanna look http://stackoverflow.com/questions/18777989/difference-between-an-interface-and-an-abstract-class/18778307#18778307 – Vimal Bera May 15 '14 at 05:42
6 Answers
Difference between abstract class and interface in Java
Abstract Class vs Interface in Java and When to use them over otherWhile deciding when to use interface and abstract class, it’s important to know difference between abstract class and interface in Java. In my opinion, following two differences between them drives decision about when to use abstract class or interface in Java.
1) Interface in Java can only contains declaration. You can not declare any concrete methods inside interface. On the other hand abstract class may contain both abstract and concrete methods, which makes abstract class an ideal place to provide common or default functionality. I suggest reading my post 10 things to know about interface in Java to know more about interfaces, particularly in Java programming language.
2) Java interface can extend multiple interface also Java class can implement multiple interfaces, Which means interface can provide more polymorphism support than abstract class . By extending abstract class, a class can only participate in one Type hierarchy but by using interface it can be part of multiple type hierarchies. E.g. a class can be Runnable and Displayable at same time. One example I can remember of this is writing GUI application in J2ME, where class extends Canvas and implements CommandListener to provide both graphic and event-handling functionality..
3) In order to implement interface in Java, until your class is abstract, you need to provide implementation of all methods, which is very painful. On the other hand abstract class may help you in this case by providing default implementation. Because of this reason, I prefer to have minimum methods in interface, starting from just one, I don't like idea of marker interface, once annotation is introduced in Java 5. If you look JDK or any framework like Spring, which I does to understand OOPS and design patter better, you will find that most of interface contains only one or two methods e.g. Runnable, Callable, ActionListener etc.
I haven't included all syntactical difference between abstract class and interface in Java here, because focus here to learn when to use abstract class and interface and choosing one over other. Nevertheless you can see difference between interface and abstract class to find all those syntactical differences.

- 996
- 1
- 13
- 26
you are free to choose interface
or abstract class
in the above scenario
but keep below things in mind
if you made a fully abstract class
then
your subclass
will extend
one class to implement that behaviour and
its not eligible to extend any other class as multiple inheritance
is not supported in java.
see without interface
we cant achieve multiple inheritance
.
Now if we approach interface
instead of fully abstract
class
then class can implement it and still eligable for extension of one more class.
so choose fully abstract
class in your design if your sub classes
can never need to extend any other class.
if sub classes
need to extend some classes later (usually its how we need in application) then choose interface
.

- 2,686
- 1
- 18
- 27
Because you can have a class implement 2 interfaces, but you can't have a class implement (or extend) 2 abstract classes.

- 8,170
- 10
- 42
- 64
You write a java program with interfaces, abstract class and concrete class but do not mention any constructor, compile it and see byte codes in .class files. You would see as expected abstract class and concrete classes have default constructor added by compiler where as interfaces do not get constructor added to it by compiler. This should help you understand technical differences as I believe you know other differences.

- 3,346
- 1
- 20
- 17
I might receive downvotes for this answer :) . Consider this scenario.
public class TestClass{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Fish f = new Fish();
f.walk();
}
}
abstract class Animal {
protected void walk() {
System.out.println("walking");
}
}
class Fish extends Animal {
}
O/ P : walking
Technically a fish can't walk. Still since it extends the Animal class, it "can" walk which is wrong from a design perspective(even though Fish doesn't implement walk(), it can still call it).. If Animal were to be an Interface, then Fish must implement the method walk() which it won't / shouldn't. So, you will be forced to relook at the design.

- 35,966
- 12
- 68
- 104
An abstract class can have some implementation code and instance variables. An interface has only method signatures and static variable definitions, no implementation code.
Further, if you implement an interface you must include implementation code for all of the methods defined in the interface.

- 164
- 10
-
2If you're going to down vote, at least provide an explanation as to what's wrong with this answer – Chad May 16 '14 at 16:30