Articles

Difference interface vs abstract class in java

Difference interface vs abstract class in java


In this tutorial, we will learn about:
  • difference between abstract class and interface in java

Abstract class and interface differ based on following parameters:
 
Methods
  • An abstract class can have both abstract methods and concrete methods.
  • An interface has all the methods as abstract. Methods in interface is 100% abstract.
Keyword used to declare
  • To declare abstract class use the keyword ‘abstract’.
  • To declare interface use the word ‘interface’. 
Method declaration
  • In abstract class, to declare any method as abstract use of abstract keyword is mandatory.
  • In interface, to declare methods as abstract use of abstract keyword is optional. Methods in interface is by default abstract.
Inheritance Supported
  • Being a class, an abstract class cannot inherit multiple classes. It can only inherit one class.
  • An Interface can extend (or inherit) multiple interfaces.
What can be extended?
  • An abstract class can extend either normal class or abstract class.
  • Interface can extend only interface but not a class.
Variable type & Access modifier
  • An abstract class can have static, non-static, final or non-final variable with any access modifier.
  • An interface can only have public static final variable. In interface variable is constant.

Example of abstract class

Given below is a simple example of abstract class:

abstract class TutorialsInHand{
      
      //abstract method- body of the method not defined 
      public abstract void javaTutorial();

      //concrete method or non-abstract method
      public void motive(){
            System.out.print("Provide best learning resources");
      }

}

Read in detail about abstract method here


Example of Interface

Given below is a simple example of interface in java

interface SampleExample{

   public static final int constVar = 9;

   //abstract method 
   public void method1();

}

Read in detail about interface in java

 


Java Interview Questions

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Sonu Pandit
Technology geek, loves to write and share knowledge with the world. Having 10+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :    Published Date : Aug 30,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!