Monday 20 November 2017

Difference between an interface and abstract class



ABSTRACT 



A class has common behavior which repeatedly use for subclass then you should go with abstract class. You can override the method of parent class & if you want apply some extra modification as per your code needs.

Abstract classes may contain abstract declarations, concrete implementations, or both.

Abstract classes are best choice for re-implementation in future that to add more functionality without affecting of end user.


  • Abstract class can have abstract and non-abstract methods.
  • Abstract class doesn't support multiple inheritance.
  • Abstract class can have final, non-final, static and non-static variables.
  • Abstract class can provide the implementation of interface.
  • The abstract keyword is used to declare abstract class.
  • Comparatively fast.
  • We can't not create object of Abstract class.
  • We can create reference variable.

We choose an abstract class when there are some features for which we know what to do, and other features that we know how to perform.

Consider the following example:

public abstract class Burger{
   
    public void packing(){
        //some logic for packing a burger
    }
   
    public abstract void price(); //price is different for different categories of burgers

}

public class VegBerger extends Burger{
    public void price(){
         //set price for a veg burger.
    }
}

public class NonVegBerger extends Burger{
     public void price(){
         //set price for a non-veg burger.
     }
}

If we add methods (concrete/abstract) in the future to a given abstract class, then the implementation class will not need a change its code. However, if we add methods in an interface in the future, we must add implementations to all classes that implemented that interface, otherwise compile time errors occur.
            


INTERFACE

Interfaces are rules. That’s because rules you must give an implementation to them that you can't ignore or avoid, so that they are imposed like rules which common understanding among the developers.

In other words, Interfaces give the idea what is to be done but not how it will be done. So implementation completely depends on developer by following the given rules.

  • Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
  • Interface supports multiple inheritance.
  • Interface has only static and final variables.
  • Interface can't provide the implementation of abstract class.
  • The interface keyword is used to declare interface.
  • Interface are slow as it requires extra indirection.

If user want to write different functionality that would be different functionality on objects. Interfaces are best choice that if not need to modify the requirements once interface has been published.

Consider a Payment class. Payment can be made in many ways, such as PayPal, credit card etc. So we normally take Payment as our interface which contains a makePayment() method and CreditCard and PayPal are the two implementation classes.

public interface Payment{
     void makePayment();//by default it is a abstract method
}

public class PayPal implements Payment{
     public void makePayment(){
         //some logic for PayPal payment
         //e.g. Paypal uses username and password for payment
     }
}

public class CreditCard implements Payment{
    public void makePayment(){
         //some logic for CreditCard payment
         //e.g. CreditCard uses card number, date of expiry etc...
     }
}

In the above example CreditCard and PayPal are two implementation classes /strategies. An Interface also allows us the concept of multiple inheritance in Java which cannot be accomplished by an abstract class.

I hope this will help!