Abstract class is a class where its implementation is incomplete or non-existent. Abstract class cannot be instantiated but can be extended. A subclass that implements an abstract class can be instantiated.
Abstract class is similar to interface but also have some differences. Both cannot be instantiated and both can contain methods without implementation. The differences are:
- a class can extend only one class but can implement many interfaces separating them with “,”
- An abstract class can define fields that are not static nor final. An interface all fields must be public, static and final
- An abstract class can define public, protected and private methods but an interface must have not implemented and public methods.
public abstract class ClassName --- fields and methods ---
An abstract method is a method declared without implementation, no body. An abstract class may or may not have abstract methods. Sub classes must override and implement the abstract methods.
public abstract returnType methodName();
Now extend another class and implement the method:
If you use IntelliJ press Alt + Enter , select Implement methods and click OK
- Java – Abstract classes & methods
- Java – Interface
- Java – class inheritance | superclass and subclass