Interfaces Vs Abstract classes



This is a very important general question in interviews. First of all you may look following table to identify main differences between Interfaces and Abstract classes. 


InterfaceAbstract class
If you only have SRS, it is better to use interfaces.
This is the first stage of creating an application.
If application is being created partially,  then it is better to use abstract classes.
Every methods in interfaces are already public and abstract.Abstract classes may have both abstract and concrete methods.
It cannot be used private, protected, static, final, native... with interfacesAny access or non access modifier can be used with abstract classes.
Every variable inside interface are always public, static and finalNo restrictions
Serialization cannot be applied to interfaces.

Because serialization is convert objects into series of bytes. But we cannot create objects using interfaces. Then serialization cannot be applied to interfaces.
No restrictions
private, protected access modifiers cannot be used for variables in interface.No restrictions
Variables should be initialized at the time of declaration.No restrictions
Instance and static blocks are not applicable for interfaces.No restrictions
Constructors are not allowed in interfaces

Because variables are already static and initialized and static. 
No restrictions


Then lets move into real world problem. I'm going to create two concrete classes, one abstract class and one interface. Here you can see them.

Human.java interface

package com.app.demo;

public interface Human {

 public void eat();
 public void walk();
 public void work();


}


OtherHuman.java abstract class

package com.app.demo;

public abstract class OtherHuman {

 public void eat(){};
 public void walk(){};
 public void work(){}; 

}



Employee.java class

package com.app.demo;

public class Employee implements Human {

 @Override
 public void eat() {
 }

 @Override
 public void walk() {
 }

 @Override
 public void work() { 
 }

}



Student.java class

package com.app.demo;

public class Student extends OtherHuman {

 @Override
 public void eat() {
 }

 @Override
 public void walk() {
 }

}



Then you may read this. This is very simple demonstration. 

  • I have mention three methods, eat(), walk() and work() In both Human interface and OtherHuman abstract class.
  • You know, methods are called behaviors of objects.
  • Then Employee can have all those behaviors eat, walk and work. But Student can have only eat and walk. Because I assume student don't have a job.


When to use Interfaces and Abstract classes ?

  • If you implement any interface, you have to override each and every methods which are in implemented interface.
  • In this example if you implement Human interface to Student, then it have to override all those methods including work() also. 
  • You cannot avoid overrinding work() method if you are implementing Human interface.
  • So this is the time to use abstract classes.
  • If you use OtherHuman abstract class instead of using Human interface, then you can choose whatever methods which you need to be included to Student class.
  • That is why I extended OtherHuman abstract class to Student class.
  • Then I can override eat() and walk() without work() method.


So I think now you have a good idea about the difference between interfaces and abstract classes. Hope you can understand. 






Interfaces Vs Abstract classes Interfaces Vs Abstract classes Reviewed by Ravi Yasas on 2:13 PM Rating: 5

No comments:

Powered by Blogger.