Java Abstract Class Interface Difference

Let’s discuss the differences and also the similarities between an Abstract Class and an Interface in terms of the Java programming language.

Abstract class will contain one or more abstract methods, which must be implemented by sub classes. Abstract class starts with the keyword abstract followed by the class definition. Abstract classes can contain one to many abstract methods.

Interface is a object that contains the method declaration but not the implementation. Any class that implements the Interfaces must provide the definition for all the methods. Interface must start with the keyword interface. Interfaces must contain all abstract methods and declarations.

Method Types

  • Abstract Class : Both Abstract & Non-Abstract Methods
  • Interface : Only Abstract Methods

Variables

  • Abstract Class : Static, Non-Static, Final, Non-Final
  • Interface : Static, Final

Implement

  • Abstract Class : Implementation of Interface
  • Interface : Can’t Provide Implementation of Abstract Class

Inheritance/Abstraction

  • Abstract Class : Extended Using Keyword extends
  • Interface : Implement Using Keyword implements

Data Access

  • Abstract Class : Can be public, protected, private
  • Interface : Default to Public


Abstract Class Sample Code

abstract class jcdAbstractClass
{
    protected String jcdString;
    
    public String getString()
    {
        return jcdString;
    }
}


Interface Sample Code

public interface jcdInterface
{
    public void jcdDoSomething();
    public int jcdInteger = 100;
}


It’s important to remember that you can’t instantiate an abstract class or an interface in Java.


Add a Comment

Your email address will not be published. Required fields are marked *