Abstract classes differ from interfaces in that abstract classes may contain concrete implementation of methods while interfaces may not.
For example, it is legal for abstract classes to have concrete method like this:
public class Abstractclass{
abstract void f1();
/**
* this is a concrete method with implementation
*/
void f2(){
System.out.println("do something");
}
}
But for interface, all methods are implicitly abstract. You cannot have concrete methods:
public interface InterfaceClass{
void f2();
void f3();
}
An interface
is like a contract to the developer with no implementation. It basically specifies the what
. The developer then implements this interface to define the how
.
An abstract
class contains some implementation useful to sub classes.