Difference between abstract class and interface

Ans) Understanding when to use abstract class or interface, helps in designing systems better and also it is one of the most common interview question. To begin with lets understand the difference.
  • A class is called abstract when it is declared with keywordabstract. Abstract class may contain abstract method. It can also contain n numbers of concrete method. Interface can only contain abstract methods.
  • Interface can have only abstract methods. Abstract class can have concerete and abstract methods.
  • The abstract class can have public, private, protected or default variables and also constants. In interface the variable is by default public final. In nutshell the interface doesnt have any variables it only has constants.
  • A class can extend only one abstract class but a class can implement multiple interfaces. Abstract class doesn't support multiple inheritance whereas abstract class does.
  • If an interface is implemented its mandatory to implement all of its methods but if an abstract class is extended its mandatory to implement all abstract methods.
  • 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.

We can re-iterate above points in a tabular form

Abstract classInteface
Type of methods At least one abstract method and can have multiple concrete methods All methods are public abstract
Variables Can have public, private, default or protected variables All variables as public static final
Inheritance Concrete class can extend only one abstract class. Class can implement multiple interface
Extends Abstract class can extend abstract class Interface can implement other interaces
It is also considered that abstract class is faster that interface, since the compiler needs to find the implementation and figure out the concrete class. But, the results show that improve in performance is very minimal and it should not be consider during building classes.

When to use abstract class over interface:

Abstract class is mainly used to:

  • Define a default behavior for subclasses. It means that all child classes should have perform same functionality.
    public abstract class VideoStreaming {
      protected void startStreaming() { //it is protected so that it can be overridden
        //start streaming
      }
      public abstract boolean authenticate(String username, String pwd);
    }
    
    public class Netflix extends VideoStreaming {
      public boolean authenticate (String username, String pwd) {
        //custom implementation
      }
    }
  • Define a flow/procedure for a use case and provide some default implementation and leave some implementation to the subclass.
    public abstract class VideoStreaming {
      public abstract boolean authenticate(String username, String pwd);
      public List<Movies> getMovies() {
        if(authenticate) {
          //get personalized movies
        }
      }
    }
    public class Netflix extends VideoStreaming {
      public boolean authenticate (String username, String pwd) {
      }
    }

Interface can be used to define a contract or behaviour. A class can implement multiple interfaces. Interface can also act as a contract between two systems to interact.

public interface ISocialLogin {
  // by default methods are public and abstract
  Credentials facbookLogin() ; 
  Credentials twitterLogin() ;
}
public class Netflix extends VideoStreaming implements ISocialLogin {
  public boolean authenticate (String username, String password) {
    //use social logins here
  }

  public Credentials facebookLogin() {
   // some implementaion
  }
  public Credentials TwitterLogin() {
   // some implementaion
  }
}

Subscribe to get latest updates.

© 2015 Java Questions | Sitemap