Abstract Class and Interfaces
A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class.
Abstract class does not support Multiple Inheritance . Interface supports Multiple Inheriatance.
Abstract class can implemented some methods also. Interfaces cannot implement methods.
In Interface all member functions are public by default but in abstract class we can have private and protected members.
With abstract classes, you are grabbing away each class’s individuality. With Interfaces, you are merely extending each class’s functionality.
Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast compared to interfaces.
Interfaces can be best the choice when your program need some global variables.
Abstract class definition begins with the keyword "abstract" keyword followed by Class definition. An Interface definition begins with the keyword "interface".
Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses. Interfaces are useful in a situation when all its properties need to be implemented by subclasses
The problem with an interface is, if you want to add a new feature (method) in its contract, then you MUST implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass
There is no difference between a fully abstract class (all methods declared as abstract and all fields are public static final) and an interface.
Neither Abstract classes nor Interface can be instantiated.
No comments:
Post a Comment