Protected by Copyscape
Powered By Blogger

Monday, August 10, 2015

Difference between Abstract Class and Interface

Both are used to achieve abstraction where we can declare the abstract methods. Abstract class and interface both can't be instantiated.

Please see the below differences between these two:

Abstract class                                             
  • 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 have static methods, main method and constructor.
  • Abstract class can provide the implementation of interface.
  • The abstract keyword is used to declare abstract class.
Interface
  • Interface can have only abstract methods.
  • Interface supports multiple inheritance.
  • Interface has only static and final variables.
  • Interface can't have static methods, main method or constructor.
  • Interface can't provide the implementation of abstract class.
  • The interface keyword is used to declare interface.
Example:
public abstract class Animal
{
     public abstract void eat();
}

Example:
public interface Animal
{
    void eat();
}

No comments: