Difference between an interface and abstract class

Java » Difference between an interface and abstract class

Abstract Class Vs Interface

  • Abstract classes may consist of abstract methods, static methods, final methods, and methods with implementation, whereas interfaces may comprise of only abstract methods, default method, and static method only, but not support methods with implementation.
  • Abstract classes can have static, non-final, final and non-static variables, whereas an interface has only constants, by default, they are static and final.
  • Abstract classes can have methods with definition, whereas interfaces not, despite this they use default methods that allow them to provide an implementation of methods.
  • Abstract classes can be created using an ‘abstract’ keyword, while the interface is created by ‘interface’ keyword.
  • Field of abstract classes supports all access modifier such as public, protected and private,  that expands the visibility mode, while interfaces have to declared as public.
  • Abstract classes can have constructors but interfaces don’t.

Java program to demonstrate the working of abstract classes

Output:

Java program to demonstrate the working of Interface

Program:

Output:

 

More Differences between abstract class and Interface:-

  1. An interface allows multiple inheritance, whereas abstract class not.
  2. An Interface is implemented by classes using ‘implements’ keyword, Whereas abstract class is extended by classes using ‘extends’ keyword.
  3. Static methods of an interface are accessed by interface name [interfaceName.StaticMethod()], while in case of abstract classes, static methods are simply accessed by class name [className.StaticMethod() , Subclassed name, not abstract Class name ].


You must be logged in to take the quiz.

error: Content is protected !!