Some of the popular interview questions are “What are differences between abstract class and interface“. “When will you use abstract class
and when will you use interface
“. So in this article ,we will go through this topic.
Table of Contents
Before going through differences between them, Lets go through its introduction.
Abstract class
Abstract classes
are created to capture common characteristics of subclasses. It can not be instantiated, it can be only used as super class by its subclasses. Abstract classes are used to create template for its sub classes down the hierarchy.
Lets take example of JDK class GenericServlet
1 2 3 4 5 6 7 8 9 10 |
public abstract class GenericServlet implements Servlet, ServletConfig,Serializable{   // abstract method       abstract void    service(ServletRequest req, ServletResponse res) ;    void init()    {        // Its implementation           }       // other method related to Servlet   } |
When HttpServlet extends Generic servlet, it provides implementation of service() method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class HttpServlet extends GenericServlet { void service(ServletRequest req, ServletResponse res) { // implementation } protected void doGet(HttpServletRequest req, HttpServletResponse resp) { // Implementation } protected void doPost(HttpServletRequest req, HttpServletResponse resp) { // Implementation } // some other methods related to HttpServlet } |
Interface:
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods
of the interface. So it is kind of signing a contract,you agree that if you implement this interface, then you have to use its methods.It is just a pattern, it can not do anything itself.
Lets take example of Externalizable Interface.
1 2 3 4 5 6 7 8 9 10 11 12 |
public interface Externalizable extends Serializable { Â Â Â void writeExternal(ObjectOutput out) Â Â Â Â Â Â Â Â Â throws IOException; Â Â Â void readExternal(ObjectInput in) Â Â Â Â Â Â Â Â Â throws IOException, Â Â Â Â Â Â Â Â Â ClassNotFoundException; } |
When you implement this interface, you have to implement above two methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class Employee implements Externalizable{  int employeeId;  String employeeName;  @Override  public void readExternal(ObjectInput in) throws IOException,  ClassNotFoundException { employeeId=in.readInt();  employeeName=(String) in.readObject();   } @Override  public void writeExternal(ObjectOutput out) throws IOException {  out.writeInt(employeeId);  out.writeObject(employeeName); } } |
Abstract class vs Interface
Parameter
|
Abstract class
|
Interface
|
---|---|---|
Default method Implementation
|
It can have default method implementation
|
Interfaces are pure abstraction.It can not have implementation at all but in java 8, you can have default methods in interface.
|
Implementation
|
Subclasses use extends keyword to extend an abstract class and they need to provide implementation of all the declared methods in the abstract class unless the subclass is also an abstract class |
subclasses use implements keyword to implement interfaces and should provide implementation for all the methods declared in the interface
|
Abstract class can have constructor
|
Interface can not have constructor
|
|
Different from normal java class
|
Abstract classes are almost same as java classes except you can not instantiate it.
|
Interfaces are altogether different type
|
Access Modifier
|
Abstract class methods can have public ,protected,private and default modifier
|
Interface methods are by default public. you can not use any other access modifier with it
|
Main() method
|
Abstract classes can have main method so we can run it
|
Interface do not have main method so we can not run it.
|
Multiple inheritance
|
Abstract class can extends one other class and can implement one or more interface.
|
Interface can extends to one or more interfaces only
|
Speed
|
It is faster than interface |
Interface is somewhat slower as it takes some time to find implemented method in class
|
Adding new method
|
If you add new method to abstract class, you can provide default implementation of it. So you don’t need to change your current code
|
If you add new method to interface, you have to change the classes which are implementing that interface
|
When to use Abstract class and interface
- If you have lot of methods and want default implementation for some of them, then go with abstract class
- If you want to implement multiple inheritance then you have to use interface.As java does not support multiple inheritance, subclass can not extend more than one class but you can implement multiple interface so you can use interface for that.
- If your base contract keeps on changing then you should use an
abstract class
. Because if your base contract keeps on changing and you still use aninterface
, you would have to change all the classes which implements that interface every time the contract changes.
Introduction of default and static methods in java 8
Oracle has tried to bridge gap between abstract class and interface by introducing concept of default and static methods in interface.So now we can provide default implementation of a method in interface and will not enforce class to implement it. I will cover these topic in my next post.
Please go through  core java interview questions for more interview questions.