Abstract Class vs Interface


Interfaces are commonly used to declare some type of behavior shared between classes and abstract classes are rather used to implement common methods used by all derived classes and to make code more legible or optimal.


Main difference is inheritance vs implementation

Abstract classes are inherited and can be instantiated only with its child classes. Only the abstract and virtual methods will be implemented in child class.

Abstract methods will be enforced on the child class whereas virtual methods are optional.

All the implementations of interface will be implemented in another class. Interface can’t have concrete implementations.

Multiple Inheritance  

Multiple inheritance can be achieved through interfaces but not abstract classes.

 Example

Car has Multimedia, ABS, Mirrors, Engines and all other parts.  If we consider each one is abstract class or interface, it has to be implemented in Car. Now Car Class can implement multiple interfaces but can’t implement multiple abstract classes.

What is Abstract Class ?

Consider an example of Car. We know common features of Car. But while buying the car we have to buy specific Car. So we don’t want to instantiate the Car. We want to inherit the Car features and some of the implementations which are different for different cars can be made Abstract methods.  

Consider A Car with Safety precautions. It should have airbags and Reverse Sensing System. Consider their implementation is same. But multimedia system function is different for different cars.
 




 Here  I have tried to instantiate the abstract class and it gives the error. We can buy a particular model but not just car without model. It is similar to IPhone. IPhone is a abstract class and IPhone6, IPhone7 implements the abstract class and its feature can be operated through those objects.


Multi Level Abstract Classes possible

We can have multi level abstract classes. 

 
Now I have written two more classes. MultimediaCar is another abstract class inheriting existing abstract class. Surprise is for MultimediaCar class it is not forcing to implement abstract methods.

Conclusion is Non Abstract classes when inherit abstract classes need to implement the abstract methods.

Access Specifiers 

Interface cannot have access specifiers but abstract classes do.

Speed

Interface is comparatively slower compared to abstract classes.

Constructors

Abstract classes can have constructors whereas the interfaces can't.


When to use Abstract classes and interfaces ? (Source DZone.com)

Consider using abstract classes if any of these statements apply to your situation:
  • You want to share code among several closely related classes.
  • 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 fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Suppose in the above example of Car. Car can be abstract classes and its behaviors can be interfaces.

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.
My take on this :

1. I will use abstract classes when I want to have re usability of some of the features and some I want to implement based on the behavior of implementing class.

2. I will go with the interface when all implementing classes have different behaviors or I want to achieve the interface segregation principle and multiple inheritance.

Why Do Interface can have property but not field or variable?

Property is similar to a method which will not take any memory. Interface will not take any memory and hence property can be used. Field will take memory and hence cannot be used in interface.


No comments:

Post a Comment

Pages