Difference Between Interface and Abstract Class
- Interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
- A class can implement multiple interfaces but it can extend only one abstract class.
- Variables declared in interface is by default final. An abstract class may contain non-final variables.
- An interface is a contract: it says, “hey, I accept things looking that way”, and the person using the interface says “OK, the class I write looks that way”. Abstract classes look a lot like interfaces, but they have something more: You can define a behavior for them. It’s more about a person saying, “these classes should look like that, and they have that in common, so fill in the blanks!”.
- Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.
- A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility.
Usage of Interface
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 inheritances.
- Interfaces should be uses if you plan to create multiple classes sharing the same methods.
- Use an interface when you want to define the role that other classes can play, regardless of where those classes are in the inheritance tree
- Interfaces should be used to define contracts (what is to be achieved, not how to achieve it). When we talk about an interface and define capabilities that we promise to provide, we are talking about establishing a contract about what the object can do.
Usage of abstract classes
Consider using abstract classes if any of these statements apply to your situation:
- An abstract class’s purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design.
- You want to share code among several closely related classes for code reusability.
- 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.
- Use an abstract class when you want to define a template for a group of sub-classes , and you have at least some implementation code that call sub-classes could use. When we talk about abstract classes we are defining characteristics of an object type; specifying what an object is.
- Abstract classes should be used for (partial) implementation. They can be a mean to restrain the way API contracts should be implemented.