Abstract classes
Beside the implementation of interfaces there is another way to describe what subclasses have to do, without necessarily to prescribe how this is to be done: the so-called extension of abstract classes.
When a superclass is much to abstract to be instantiated meaningfully, it should be forbidden to create objects of this class. This can be done by declaration of abstract classes. Only subclasses that extend these abstract classes and are not themselves beeing abstract can be instantiated.
UML class diagram: extension of an abstract class
Class Account is abstract and cannot be instantiated:
public abstract class Account { // abstract class because at least one method is declared as abstract protected double balance; // attributes are possible Account(double initialCredit) { // constructors are possible balance = initialCredit; } public double getBalance() { // concrete method, will be inherited return balance; } public abstract int transaction(double amount); // abstract method, has to be implemented by subclasses }
An abstract class is implemented by the use of keyword extends:
public class SavingsAccount extends Account { // keyword "extends" is needed for implementation of abstract class private double interestRate; SavingsAccount(double initialCredit, double interestRate) { super(initialCredit); // call of superclass constructor this.interestRate = interestRate; // additional code } public int transaction(double amount) { if (balance - amount <= 0) return -1; else { balance += amount; return 0; } } }
public class CheckingAccount extends Account { // keyword "extends" CheckingAccount(double initialCredit) { super(initialCredit); // call of superclass constructor } public int transaction(double amount) { // method has to be implemented, because was declared abstract balance += amount; // in superclass return 0; } }
public class Main { public static void main(String[] args) {Account account =new Account(0);// 'Account' is abstract, cannot be instantiated SavingsAccount savingsAccount = new SavingsAccount(50, 2.5); CheckingAccount checkingAccount = new CheckingAccount(0); System.out.println("Balance of savingsAccount: " + savingsAccount.getBalance()); // inherited method System.out.println("Balance of checkingAccount: " + checkingAccount.getBalance()); } }
So, why not simply implement interfaces in subclass? An interface is abstract too and can not be instantiated?
The answer is, an abstract class can define concrete methods, which don't have to be defined in subclass.But meanwhile to overcome this disadvantage of interfaces, Java 8 has introduced the concept of default
methods which also allow the interfaces to have methods with implementation: (Since Java 9 also private and static methods can be declared in interfaces.)
public interface IAccount { // Definition of an interface int INITIAL_BALANCE = 50; // attributes are public, static and final by default double balance = 0; // balance does not work, because it would be constant (beeing final) int transaction(double amount); // no definitions of method only deklarations default double getBalance() { // keyword "default" allows definition in interface >= JDK8 return balance; } }
Default implementation of static method in an interface is possible, but balance as variable attribute would not work because of beeing final.
So, is there still a need of abstract classes?
Yes it is, since an abstract class can have a state, but an interface cannot. In other words an abstract class can have non-static and non-final attributes for saving a state.
| Feature | Abstract Classes |
Interfaces |
|---|---|---|
| Instantiable | no | no |
| Type of methods |
abstract non-abstract static non-static |
abstract default methods (>= JDK8) static (>= JDK9) |
| Accessibility of methods | protected public |
public private (>= JDK9) |
| Type of attributes |
final non-final static non-static |
final static |
| Accessibility of attributes |
private protected public |
public |
| Can implement/extend |
multiple interfaces one abstract class |
multiple interfaces |
| Multiple implementation |
no | yes |
| Constructor | yes | no |