Abstract non-access modifier

0

In the last article, I showed you examples of final. In this article, I will introduce you to abstract non-access modifier.

An abstract class can never be instantiated as it is considered incomplete and expects one of its sub-classes to complete it.

Example:

package javarticles.finalmodifier;

public abstract class AbstractAction {    
}

Since AbstractAction is an abstract class, we won’t be able to instantiate AbstractAction.

    public void testAbstractInstantiation() {
        AbstractAction action = new AbstractAction();
    }

Compiler issues error Cannot instantiate the type AbstractAction

You can only instantiate through a non-abstract sub-class like the Creator below:

package javarticles.abstractmodifier;

import javarticles.abstractmodifier.AbstractAction;

public class Creator extends AbstractAction {

}
    public void testConcreteClassInstantiation() {
        Creator creator = new Creator();
    }

You can declare a class abstract even without any abstract methods. A Class becomes abstract moment you add abstract keyword before the class.

Let’s add an abstract method doSomething() to class AbstractAction

package javarticles.abstractmodifier;

public abstract class AbstractAction { 
    abstract void doSomething();
}

Since Creator class extends AbstractAction, it has to implement the new method else compiler will issue an error:
The type Creator must implement the inherited abstract method AbstractAction.doSomething()

If a class contains even one abstract method, the whole class must be declared abstract.

In order to the fix the above compilation error, you may need to either declare Creator as abstract or implement the abstract.
For example:

package javarticles.abstractmodifier;

public abstract class Creator extends AbstractAction {
}

or implement doSomething()

package javarticles.abstractmodifier;

public class Creator extends AbstractAction { 
    void doSomething() {
        System.out.println("Create something");
    }
}

You are also allowed to put non-abstract methods in an abstract class or implement the existing abstract method and introduce new abstract methods.

For example:

package javarticles.abstractmodifier;

public abstract class AbstractCreator extends AbstractAction {
    void doSomething() {
        System.out.println("let the creator create something");
        createSomething();
    }
    
    abstract void createSomething();
}

An abstract class has to be subclassed else it will remain incomplete whereas a final class is sealed and complete and doesn’t expect any other class to extend it so you must not combine both these modifiers else compiler will issue error:

package javarticles.abstractmodifier;

public abstract final class AbstractCreator extends AbstractAction {
    void doSomething() {
        System.out.println("let the creator create something");
        createSomething();
    }
    
    abstract void createSomething();
}

The class AbstractCreator can be either abstract or final, not both

About Author

Ram's expertise lies in test driven development and re-factoring. He is passionate about open source technologies and loves blogging on various java and open-source technologies like spring. You can reach him at rsatish.m@gmail.com

Leave A Reply