Java Abstract Classes and Methods

In Java, the abstract keyword can be used with classes and methods; but not with variables. The abstract is a non-access modifier that helps in achieving abstraction in object-oriented designs.

public abstract class BaseController {  //abstract class

	abstract void process();  //abstract method
}

1. Abstract Class

In Java, an abstract class cannot be instantiated due to its partial implementation, but it can be extended just like a normal class.

When an abstract class is inherited, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

public abstract class BaseController {  

    abstract void process();  
}

public class AccountController extends BaseController {  

    void process() {
        //method implementation
    }

    //...
}

2. Abstract Method

An abstract method is declared without an implementation i.e. without curly braces, and followed by a semicolon. If a class includes abstract methods, then the class itself must be declared abstract.

public abstract class BaseController {  //abstract class

  abstract void process();  //abstract method

  void displayHelp() {  //non-abstract method
  
    //method body
  }      
}

It is worth noticing that methods defined in an interface, that are not declared as default method or static, are implicitly abstract so the abstract modifier is not used with interface methods.

public interface IReport {  

  Object[][] generate(Object[][] inputArr);    //abstract is implicitly inferred by compiler
}

Please note that an abstract method can NOT be final, native, synchronized, static or private. It is very obvious because these keywords control the behavior of the method, and abstract methods do not have any behavior or body.

In the following example, the code will not compile because final keyword does not allow the method to be inherited in the child class, and abstract keyword demands the same. It is a conflict. So the compiler gives error.

public abstract class BaseController { 

    final abstract void process();  //Compiler Error
}

3. Java Abstract Keyword Example

Let’s see an example of abstract keyword. In given example, we have an abstract class Animal which has one abstract method makeNoise().

This class is inherited by two child classes i.e. Dog and Cat. Both classes implement the method makeNoise() according to their nature.

public abstract class Animal {
    public abstract void makeNoise();
}
public class Cat extends Animal {

    @Override
    public void makeNoise() {
        System.out.println("meow");
    }
}
public class Dog extends Animal {

    @Override
    public void makeNoise() {
        System.out.println("bark";);
    }
}

Let’s test the above classes.

public class Main {
    public static void main(String[] args) {

        Animal a1 = new Cat();
        a1.makeNoise();

        Animal a2 = new Dog();
        a2.makeNoise();
    }
}

We can see the following output and confirm that each class implemented its own behavior in the child class.

meow
bark

4. Conclusion

  • The abstract keyword is a non-access modifier.
  • The abstract keyword can be used with methods and classes.
  • We cannot instantiate abstract classes.
  • Any subclass of an abstract class must either implement all of the abstract methods in the parent class, or be declared abstract itself.
  • Any class that contains one or more abstract methods must also be declared abstract.
  • The abstract keyword cannot be used with final, native, synchronized, static or private keywords.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode