Java: Abstract method in Easy Way with Real-Time Examples

Learn the abstract method in Java, clear definition, Where to Use and why to use in a program with super simple real-time examples.

What is an Abstract method in Java?

A method that has only a declaration and no implementation (no body) is known as an abstract method.

The keyword “abstract” is used in a method declaration to make a method abstract as shown below.

abstract void addIngredient();

Where to use the Abstract method in Java?

The abstract methods are used in an interface or an abstract class in Java as shown below.

interface Charger {
	void charge(); // By default abstract
}


abstract class Beverages {

	abstract void addIngredient();//abstract method has no body

}

Note that the above methods the charge() and the addIngredient() do not have definition or body as below but declaration only.

abstract void addIngredient(){
}

Also, note that the method in an interface is by default abstract. However, there is nothing wrong writing abstract before method in an interface but its redundant.

interface Charger {
	abstract void charge(); // By default abstract
}

How to use the Abstract method in Java?

Abstract methods are designed to be implemented by subclasses that extend the abstract class or implement the interface.

Here is how a class in java can use the abstract method of an abstract class. A class can inherit or extends the abstract class and implement the abstract method.

Extending the abstract class:

The Tea class extends the Beverages class and overrides and defines the abstract method addIngredient().

class Tea extends Beverages{

	@Override
	void addIngredient() {
		// Do Something		
	}	
}

Implementing the interface:

The Mobile class implements the Charger interface and overrides and defines the abstract method charge().

class Mobile implements Charger {

	@Override
	public void charge() {

	}
}

In fact, an abstract method in an interface or an abstract class is used to enforce subclasses to implement (override and define) it.

NOTE:

I)An abstract method must be implemented by derived/sub-classes. For example,

class Tea extends Beverages{

	@Override
	void addIngredient() {
		// Do Something		
	}	
}

If the subclasses don’t implement the abstract methods when they extend the abstract class or an interface, the compiler will flash error.

II) All the methods in an interface are by default abstract.

Why use the Abstract method in Java?

Whenever we want to force sub-classes to define a method, we use an abstract method. For example, via an interface, you want to force sub classes to define/implement methods, hence the methods of an interface are abstract.

In abstract class, you want to define some methods but besides that, you want to force sub-classes to implement some other methods, you make those methods abstract.

Complete Code Examples of abstract methods

Let’s see the simple but complete code examples of using an abstract methods of an interfaces or an abstract class in a Java programming.

Abstract Method Example in Java of an abstract class:

In the example below, the abstract class Beverages has a defined method addMilk() and an abstract method addIngredient(). The subclass Tea will use the addMilk defined method and define and implement the abstract method addIngredient() for its own.

abstract class Beverages {
	// Defined
	public void addMilk() {

		System.out.println("Sugar...");
	}

	// abstract
	abstract void addIngredient();
}

class Tea extends Beverages {

	@Override
	void addIngredient() {
		System.out.println("Tea...");
	}
}

public class Sample {

	public static void main(String[] args) {

		Tea t = new Tea();
		t.addMilk();
		t.addIngredient();
	}
}

Output:

Sugar…

Tea…

Abstract Method Example in Java of an Interface:

interface Charger {
	void charge(); // By default abstract
}

class Mobile implements Charger {

	public void display() {
		System.out.println("Screen...");
	}

	@Override
	public void charge() {

		System.out.println("Charging...");
	}
}

public class Test1 {

	public static void main(String[] args) {

		Mobile m = new Mobile();
		m.display();
		m.charge();
	}
}

Output:

Screen…

Charging…

Abstract method important points:

  1. If an interface or an abstract class contains multiple abstract methods, all the abstract methods must be implemented by the subclasses.
  2. All the methods in an interface are by default abstract.

Leave a Comment