Difference between Interfaces and Abstract Classes

What is the differences between interface and abstract classes?

This is one of the most widely asked question in an interview at any level. In this post we are going to focus on differences between interface and abstract classes, what are ideal situations in development for using these etc. 

While designing a software or an API we don’t want Base class to be instantiated directly, we just want the base class to provide a structure that needs to be implemented by sub classes. This can be achieved using abstract classes. Interface takes this concept one step further by preventing any method or function implementation at all. 

Differences between interfaces and abstract classes :

1. Prior to Java 8 interfaces can only have abstract methods without any implementation but java 8 on wards we can now have default implementation of methods in interfaces and for that we can use default keyword at the beginning of method signature. Abstract classes can have abstract as well as non abstract methods.

2. Variables declared in interfaces are final by default whereas abstract classes can have non-final variables as well. 

3. Abstract classes can extend interfaces but reverse is not possible.

4. In java we implements interfaces using implements keyword whereas we extend abstract classes by using extends keyword.

5. We can implement more than one interface but we can not extend more than one abstract class.

6. Members of interface in java are public by default, members of abstract classes can have any access specifier.

Diamond problem and use of interface. 

Consider a secenario where we are designing an API for drawing different shapes. First let’s design this using abstract classes. We have created a parent class Shape with all necessary functions that needs to be implemented by it’s sub classes. Now let's create two sub classes one is Circle and other is Square and both extends Shape class. Now what if we want to create a new shape Circle on Square we can’t reuse Circle and Square classes here because only one class can be extended. 
This is know as diamond problem, we can use interfaces to resolve this type of design problems. 

When to use abstract class?

In  case  where  we want  to  use  implementation inheritance  then it  is usually provided by  an  abstract  base class.  Abstract  classes are  excellent  candidates inside of  application frameworks.  Abstract  classes  let  us define  some default  behavior and force subclasses to  provide any  specific behavior.  Care  should be taken not  to  overuse implementation inheritance.

When to use interface?

For polymorphic interface inheritance,  where the client  wants to  only  deal  with  a type  and  does  not  care  about  the  actual  implementation  use  interfaces.  If  you  need  to  change  your design frequently,  you  should  prefer  using interface to  abstract.  Coding  to  an interface inheritance can achieve  code  reuse  with  the help of  object  composition.  For example:  The Spring  reduces coupling  and framework’s  dependency  injection promotes  code to  an  interface principle.  Another justification for using  interfaces is that  they  solve the  ‘diamond problem’ of  traditional  multiple  inheritance as  discussed above. Java  does  not support  multiple  inheritance.  Java only supports  multiple interface inheritance. Interface  will solve  all the ambiguities caused by  this ‘diamond  problem’. 

What is marker interface?

The interfaces  with  no  defined  methods  act  like markers.  They  just  tell  the  compiler that  the  objects  of  the classes implementing  the interfaces with  no defined  methods need  to  be  treated  differently.  Example  java.io.Serializable,  java.lang.Cloneable,  java.util.EventListener  etc.  Marker interfaces are also  known  as “tag” interfaces  since they  tag  all the derived  classes into  a category based on their purpose.
Share:

Related Posts:

No comments:

Post a Comment

Popular Posts

Recent Posts

Followers

Total Pageviews

5,030