Java abstract keyword – abstract classes and methods

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

1. Java abstract class

Abstract classes cannot be instantiated due to their partial implementation, but they can be inherited 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 DemoClass 
{
	//declare other methods and fields
}

2. Java abstract method

An abstract method is a method that 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.

Methods in an interface, that are not declared as default or static, are implicitly abstract so the abstract modifier is not used with interface methods.

public abstract class DemoClass 
{
	//declare other methods and fields

	//an abstract method
	abstract void someMethod();
}

Please note that an abstract method can NOT be final, native, synchronized, static or private.

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("Meaaauu...");
    }
}
public class Dog extends Animal 
{
    @Override
    public void makeNoise() 
    {
        System.out.println("Bho Bho...");
    }
}

Let’s test above classes.

public class Main 
{
    public static void main(String[] args) 
    {
        Animal a1 = new Cat();
        a1.makeNoise();

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

Program output.

Meaaauu...
Bho Bho...

4. Summary

  • abstract is a non-access modifier.
  • 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 super-class, or be declared abstract itself.
  • Any class that contains one or more abstract methods must also be declared abstract.
  • abstract keyword cannot be used with final, native, synchronized, static or private keywords.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.