In this article, we will list the difference between interface and abstract classes in Java
Before moving ahead with the differences, read the detailed concepts about Interface and Abstract classes in the following articles
Let us detail out difference between interface v/s abstract classes in tabular form below,
1. Interface v/s Abstract Classes
Sr. No. | Interface | Abstract class |
1 | All methods inside interface are implicitly abstract
Interface helps to achieve 100% abstraction | Abstract class can contain both abstract method and concrete method
Commonly used in factory design pattern |
2 | Constructor is not allowed in Interface | Abstract class can have constructor; both default and parameterized constructor allowed |
3 | Interface can’t be instantiated | Abstract classes too can’t be instantiated even though having constructor with instance data members |
4 | Implicitly, all methods declared inside interface are public | An abstract classes can have abstract methods with protected or public access-modifier |
5 | An interface can extend any number of other interfaces | An abstract class can extend only one class; it could be either concrete or another abstract class |
6 | Interface is a mean by which Java supports multiple inheritance | Abstract class doesn’t support inheritance |
7 | abstract keyword for methods inside interface are optional | abstract keyword is must for abstract methods inside abstract class |
8 | Variables defined inside interface are implicitly public, static and final i.e.; CONSTANT | Variables can be static or final or both with any access modifier |
9 | No such things is allowed in abstract classes | An abstract class can have static main() method to execute the class |
10 | Only abstract methods allowed | Static methods are allowed inside abstract class |
2. Java 8
- Post Java 8 release, Interface can have default and static method with implementation
Let us see the difference post Java 8 release,
2.1 Abstract class v/s Interface in Java 8
- At a very high level, it looks very similar but actually they are different in many ways
- Also, considering the fact that default method in interface helps us to achieve loose coupling and backward compatibility
Sr. No. | Abstract Classes | Interface |
1 | Contains members variables | All variables are actually constants i.e.; public, static and final |
2 | It can have constructors | Interface cannot have constructors |
3 | Can hold state of an object using instance member variables | Since, all variables are static and final therefore no concept of holding state of an object |
4 | Forces to implement abstract methods or else declare class as abstract | default methods can be overridden, if required but never forces |
5 | Concrete methods are allowed; in addition to abstract methods | Concrete methods should be either default or static; otherwise only abstract methods are allowed |
3. Example for Interface and Abstract class
3.1 Example on Interface
- Variable are implicitly public, static and final; and
- methods are implicitly public and abstract
Java7Interface.java
package in.bench.resources.itf.example;
public interface Java7Interface {
// variables are implicitly public, static and final
String ORGANIZATION_NAME = "ABC Pvt. Ltd.";
// methods are implicitly public and abstract
void display();
}
3.2 Example on Interface in Java 8
- variables are implicitly public, static and final;
- methods are implicitly public and abstract for methods without implementation
- Other than public abstract methods; default and static method with implementation are allowed inside Java 8 interface
InterfaceInJava8.java
package in.bench.resources.itf.example;
// only public and abstract are permitted
public interface InterfaceInJava8 {
// old abstract method
void displayAbstractMethod(); // by default public and abstract
// default method with concrete implementation from Java 8
default void displayDefaultMethod() {
System.out.println("InterfaceInJava8 : "
+ "default method impl inside interface");
}
// static method with concrete implementation from Java 8
static void displayStaticMethod() {
System.out.println("InterfaceInJava8 : "
+ "static method impl inside Java Interface");
}
}
3.3 Example on Abstract class and Abstract method
- variables can be both instance and static data members; methods can be abstract or concrete or static
- Though constructor are allowed, instantiation of abstract classes are not allowed but can have main() method and execute as well
AbstractExample.java
package in.bench.resources.abstractclass.example;
// abstract class
public abstract class AbstractExample extends DemoAbstract {
String demoString;
static int demoCounter = 1;
// default no-arg constructor
AbstractExample(){
// do some initialization logic here
}
static void staticMethod() {
System.out.println("AbstractExample: "
+ "static methods are allowed inside abstract class");
}
// abstract method declaration in abstract class
abstract void myAbstractMethod();
// concrete method definition in abstract class
void myConcreteMethod() {
System.out.println("AbstractExample: "
+ "This is my concrete method in abstract class");
}
// main() method - the entry point to JVM to begin execution
public static void main(String args[]) {
System.out.println("Accessing static field demoCounter : "
+ AbstractExample.demoCounter);
staticMethod();
}
}
4. Points to remember about Interface and Abstract classes & methods:
4.1 Interface:
- Interface is 100% pure abstraction but post Java 8 release, default and static methods are allowed
- Interface is a mean by which Java supports multiple inheritance
- Constructor are not allowed inside interface
- Instantiation of interface is not allowed
- All variables are implicitly public, static and final
- Apart from default and static methods, method with no implementation are implicitly public and abstract (old abstract method)
- Interface can be used as reference variable for sub-class instantiation
4.2 Abstract Class:
- An abstract class is declared with abstract keyword in class declaration
- Extending or sub-classing abstract class must provide implementation details to all abstract methods
- Else make extending class as abstract which means next implementing class must provide concrete implementation for all abstract methods
- An abstract class can contain mix of both i.e.; abstract methods & concrete methods
- It can have both default & parametrized constructor, but still it cannot be instantiated
- An abstract class with no abstract method is denote that this class cannot instantiated, rather we can create object of type of extending class (sub class)
- An abstract method is not allowed inside concrete class and compiler throws error stating “requires method body instead of a semicolon(;)”
- Both static and non-static fields can be declared inside abstract class
- Abstract class can extend only one class, it could be either an abstract class or concrete class
- An abstract can have main() method – the entry point to JVM to begin execution
4.3 Abstract Method:
- An abstract method has no body
- Method signature ends with semicolon(;)
- Abstract method can throw exception
- Abstract methods can be declared inside abstract class only
- Abstract methods cannot be final or private or static
- Only protected, default and public access modifiers allowed
Related Articles:
- Abstract classes and methods in Java
- Interface in Java
- Interface in Java 8
- Java Constructor with example
- Initialization blocks
- Interview Question and Answers on Abstract classes and methods
References:
- https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
- http://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html
- http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
- http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html#static
- https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
Happy Coding !!
Happy Learning !!