Java Abstract Class

In this article we’ll talk about what is Java abstract class and what are its’ main characteristics. In addition to that you’ll read about when to use an abstract class in java. As a final step, we’ll take a look at some example code.

What is Java Abstract Class?

Abstract class in Java is a class that is declared abstract by using an abstract keyword. Abstract keyword is declared before the keyword class.

public abstract class Loan {}

An abstract class cannot be instantiated, similarly to interfaces in Java. Therefore, it has to be subclassed for it to have any use. An abstract class can contain one or more abstract methods. To refresh your mind, abstract methods are methods without an implementation. When a class defines at least one abstract method, it has to be declared abstract. Every subclass of an abstract class has to implement all the abstract methods an abstract parent class has defined, unless they are abstract classes themselves.

In addition to abstract methods, we can also define methods with a body and instance variables (non static variables) in abstract classes. Also, abstract classes can have constructors.

When to Use Java Abstract Class?

You should use an abstract class when you have several closely related classes and:

  • You want to share code among them.
  • They have multiple common fields and methods.
  • Their (common) fields require access modifiers other than public (e.g. protected, private).

For example, HashMap and TreeMap extend AbstractMap that is an abstract class. AbstractMap defines several common methods for its’ implementing classes that HashMap and TreeMap can use.

In constrast, you should use an interface instead when you have several unrelated classes but they have some common behaviour.

Java Abstract Class Example

In this example we’ll have two common classes that are going to share some code among them: BusinessLoan and PrivateLoan. In order for them to share some common code, we need to define an abstract class called Loan.

BusinessLoan and PrivateLoan have a common field – DEFAULT_INTEREST_RATE. This field and the getter for it we will define in the Loan class. The difference between two loans is that we charge a different interestRate, for business loans we have a field named nameOfBusiness and for private loans we have a field nameOfPerson.

Full example:

public abstract class Loan {

    // Define default interest rate for loans
    private static final double DEFAULT_INTEREST_RATE = 5.00;

    // Abstract method for classes to implement
    public abstract double getInterestRate();

    protected static double getDefaultInterestRate() {
        return DEFAULT_INTEREST_RATE;
    }
}
public class BusinessLoan extends Loan {

    private static final double INTEREST_DISCOUNT = 2.00;

    // Field only used for business loans
    private final String nameOfBusiness;

    public BusinessLoan(final String nameOfBusiness) {
        // Check that name of the business is not null
        // and set it
        this.nameOfBusiness = Objects.requireNonNull(nameOfBusiness);
    }

    // Overriding the Loan class interestRate
    // Interest rate = default rate - interest discount
    @Override
    public double getInterestRate() {
        return getDefaultInterestRate() - INTEREST_DISCOUNT;
    }

    public String getNameOfBusiness() {
        return this.nameOfBusiness;
    }
}
public class PrivateLoan extends Loan {

    // Field only used for private loans
    private final String nameOfPerson;

    public PrivateLoan(final String nameOfPerson) {
        // Check that name of the person is not null
        // and set it
        this.nameOfPerson = Objects.requireNonNull(nameOfPerson);
    }

    // Overriding the Loan class interestRate.
    // Private loans have the default interest rate
    @Override
    public double getInterestRate() {
        return getDefaultInterestRate();
    }

    public String getNameOfPerson() {
        return this.nameOfPerson;
    }
}

As a final step lets’ create a private loan and a business loan and see the output of the code:

PrivateLoan privateLoan = new PrivateLoan("John Smith");

System.out.println(
        "Loan taker name: " + privateLoan.getNameOfPerson() + ", "
                + "Interest Rate: " + privateLoan.getInterestRate()
);

BusinessLoan businessLoan = new BusinessLoan("Great Business Ltd");

System.out.println(
        "Business name: " + businessLoan.getNameOfBusiness() + ", "
                + "Interest Rate: " + businessLoan.getInterestRate()
);

Output:

Loan taker name: John Smith, Interest Rate: 5.0
Business name: Great Business Ltd, Interest Rate: 3.0

Conclusion

Abstract class in Java is a class that is declared abstract and cannot be instantiated. You should use an abstract class when you have closely related classes that share some common code among them.  We can define one or more abstract methods (methods without body) in an abstract class. All of those methods have to be implemented by the classes that extend the abstract class, unless they are declared abstract themselves.

Be the first to reply

Leave a Reply

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