Monday, 24 November 2014

Difference between Abstract Class and Interface in java

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
view plainprint?
  1. public abstract class GenericServlet implements  Servlet, ServletConfig,Serializable{
  2. // abstract method
  3.     abstract  void     service(ServletRequest req, ServletResponse res) ;
  4.     void init()     {
  5.          // Its implementation
  6.      }
  7.        // other method related to Servlet
  8. }
When HttpServlet extends Generic servlet, it provides implementation of service() method
view plainprint?
  1. public class HttpServlet extends GenericServlet
  2. {
  3.        void  service(ServletRequest req, ServletResponse res)
  4.        {
  5.        // implementation
  6. }
  7.      protected  void  doGet(HttpServletRequest req, HttpServletResponse resp)
  8. {
  9.     // Implementation
  10. }
  11.    protected  void  doPost(HttpServletRequest req, HttpServletResponse resp)
  12. {
  13.     // Implementation
  14. }
  15. // some other methods related to HttpServlet
  16. }

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.
view plainprint?
  1. public interface Externalizable
  2. extends Serializable
  3. {
  4.     void writeExternal(ObjectOutput out)
  5.             throws IOException;
  6.     void readExternal(ObjectInput in)
  7.             throws IOException,
  8.             ClassNotFoundException;
  9. }
When you implement this interface, you have to implement above two methods.
view plainprint?
  1. public class Employee implements Externalizable{
  2.  int employeeId;
  3.  String employeeName;
  4.  @Override
  5.  public void readExternal(ObjectInput in) throws IOException,
  6.  ClassNotFoundException {  employeeId=in.readInt();
  7.   employeeName=(String) in.readObject();
  8.  } @Override
  9.  public void writeExternal(ObjectOutput out) throws IOException {
  10.   out.writeInt(employeeId);
  11.   out.writeObject(employeeName); }
  12. }

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
Constructor 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

No comments:

Post a Comment