What is the difference between abstract class and interface? When to use what?
tag: java-faq
Methods Interface can have only abstract methods, can’t provide the implementation. Abstract class can have abstract and non-abstract methods, can provide implementation,
Variables Interface variables are by default static & final, that's means public by default. Abstract class can have final, non-final, static and non-static variables, so it has "data hiding” facility.
Consider using abstract classes if any of these statements apply to your situation:
- there are some related classes that need to share some lines of code;
- you need non-static or non-final field(s);
Consider using interfaces if any of these statements apply to your situation:
- It is total abstraction, All methods declared within an interface must be implemented by the class(es) that implements this interface.
- A class can implement more than one interface.