Abstract Class in Java Tutorial

In this section, we will learn what the abstract class is and how it works in Java.

Java What Is Abstract Class?

An abstract class is a class in the first place! It can have methods, fields, and constructors just like any other class you’ve seen so far.

Other than the members mentioned above, an abstract class has another member called abstract method! The signature of such a method is the same as a typical method, except there’s nobody for an abstract method.

Basically, a method with no pair of curly braces `{}`.

So now the question is why an abstract class then?

Well, an abstract class is designed to be a base class for other classes to inherit from! It specifies a set of members that other classes can have and members that they MUST HAVE!

The typical public and protected members (fields and methods) of an abstract class are those that child classes can access and use them if they want to. On the other hand, abstract methods in abstract classes are those that each and every class that inherit from an abstract class, must have. Basically, these abstract methods should be overridden in the child classes and they must provide a body for those methods.

So if you think about it, abstract classes are the force modules we want to put a set of classes (by making them the child of an abstract class) in order to make sure they have at least a minimum set of members (these members are the abstract methods in the abstract classes that each child class should override and provide a body for it).

Remember: we can’t create objects from abstract classes! Their constructors could only be used within the constructors of the child classes via the `super()` call. Basically, they only can be initialized within the body of a child class in its constructor.

How to Define Abstract Class in Java?

public abstract class ClassName{/*...*/}

In order to create an abstract class, we use the `abstract` keyword before the `class` keyword and after the access specifier (if any).

Example: creating abstract class in Java

package com.example.demo;

public abstract class Abs {
    private String name;
    private String lastName;
    public Abs(String name, String lastName){
        this.name = name;
        this.lastName = lastName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

This is an abstract class and for this particular example we didn’t use any abstract method in it, there’s no difference between this one and any other typical class except for the keyword abstract that we’ve used at the beginning of this class.

Java Abstract Methods

As mentioned before, an abstract class can have abstract methods in it. Basically, the main purpose of an abstract class is to have abstract methods in it!

Any child that inherit from an abstract class much override these abstract methods and provides a body for them.

Note: you can’t create an abstract method in a class that is not abstract!

Let’s see how to create an abstract method now.

How to Define Abstract Method in Java?

Access-modifier abstract datatype methodName();

  • – First, start with the access-specifier of the method (if any).
  • – Then comes the keyword `abstract`.
  • – After that is the datatype of the target method.
  • – Then the name of the method.
  • – At the end we put a semicolon `;` to end the target abstract method.

Note: there’s nobody for an abstract method in an abstract class.

Example: creating abstract method in Java

package com.example.demo;

public abstract class Abs {
    private String name;
    private String lastName;
    public Abs(String name, String lastName){
        this.name = name;
        this.lastName = lastName;
    }
    public abstract void printFullName(); 
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

In this example, the abstract method is:

public abstract void printFullName();

As you can see, there’s nobody of this method in the abstract class! It’s the job of the child classes to provide a body of an abstract class.

Example: Java abstract class and inheritance

package com.example.demo;

public abstract class Abs {
    private String name;
    private String lastName;
    public Abs(String name, String lastName){
        this.name = name;
        this.lastName = lastName;
    }
    public abstract void printFullName();
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}


package com.example.demo;

public class Child extends Abs{

    public Child(String name , String lastName){
        super(name, lastName);
    }

    @Override
    public void printFullName() {
        System.out.println(getName()+" "+getLastName());
    }
}


package com.example.demo;

public class Main {

    public static void main(String[] args) {
        Child child = new Child("John","Doe");
        child.printFullName();
    }
}

Output:

John Doe

Here the `Child` class inherited from the `Abs` class and overrides the `printFullName()` method by defining a body of the method.

If it didn’t the compiler would’ve returned error.

Can we use both abstract and final keyword together on a method or class?

No! The purpose of the `final` keyword on a class is to stop it from being inherited via other classes! Also using the final keyword on a method is to stop that method from being overridden in the child classes.

This is on the opposite side of abstract classes and methods! Basically, the purpose of using abstract class or abstract method is to be used by child classes.

So no! Using the final and abstract keywords is an illegal combination.

Why can’t we create an object of an abstract class?

Because abstract classes mostly contain abstract methods and such methods as you saw before, don’t have bodies to be invoked! Abstract classes are not fully defined.

Basically, the purpose of abstract classes is to provide a structure for child classes only! They are not designed to be used for direct object creation.

Abstract class can have constructor

Yes! Abstract classes can have constructors as well! But these constructors can only be used in the child classes using the call to the `super()` statement.

We know that when the execution engine creates an instance from a child class, it will initialize the members of its parent classes as well. By default the execution engine puts the default values for the members of the parent classes, but if we have an abstract class and want to initialize it with specific types of values, we can use the `super()` statement within the body of the target constructor of the child class and that way, the execution engine knows how and with what values to initialize the members of an abstract class.

Note: check the Java inheritance and constructors if you’re not familiar with how initialization work in Java.

Example: abstract class with constructor

package com.example.demo;

public abstract class Abs {
    private String name;
    private String lastName;
    public Abs(String name, String lastName){
        this.name = name;
        this.lastName = lastName;
    }
    public abstract void printFullName();
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}


package com.example.demo;

public class Child extends Abs{

    public Child(String name , String lastName){
        super(name, lastName);
    }

    @Override
    public void printFullName() {

    }
}

Here the Child class extended the `Abs` class and in the body of the Child constructor we can see the call to the constructor of the Abs class with the specified values using the `super()` statement.

Now if we create an instance of the `Child` class and assign the values of the `name` and `lastName`, these two values will be passed to the constructor of the `Abs` class and assigned to the fields of this class.

Interfaces vs Abstract Classes: Difference between Abstract Class and Interface in Java

Interfaces are designed so that any class that implement it, has to override its methods and define bodies for the methods in the target interface. Also, we can’t set constructors for interfaces.

On the other hand, an abstract class can have constructors, fully defined methods and, if needed, abstract methods as well. Basically, we can have an abstract class without any abstract method in it and so there won’t be any pressure on the child classes to override a method of an abstract class.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies