Wednesday 8 May 2013

Difference between an Interface and an Abstract Class ???

Let's first summarize the two of them ...

An Interface - is much like an agreement that declares the characteristics and capabilities of a Class that implements it.

An Abstract Class - is more like a conceptual subset of a class that extends it.

Now, Lets talk about them in depth ...

Abstract Classes ...
  • cannot be instantiated
  • have abstract methods i.e only declaration part of a method
  • can also have non-abstract methods
  • can have data members as well
  • must be subclassed to be used
  • must be declared as abstract
  • can extend other classes
  • can implement interfaces
public abstract Class Automobile {
   private int wheels;
   public abstract travel();
}    

Interface ...
  • is much like 100% abstract classes
  • cannot be instantiated
  • data members are implicitly public static final
  • methods are implicitly public abstract
  • can only extend other interfaces, it cannot extend a class or implement an interface
  • can be used polymorphically
  • can be implemented by any class within any hierarchy

    public interface moveable {
       int wheels;
       travel();
    }    

No comments:

Post a Comment