Abstract Class in Java with Purpose and Real Time Example

Learn the abstract class in Java with crystal clear definition, purpose with real time example, code, uses and important points.

Abstract Class in Java

The abstract class is designed to contain both the defined and abstract methods. This class may contain only defined methods OR only abstract methods OR both.

NOTE: Recommend reading first, the abstract method in java given here even you are familiar with it for better understanding the abstract class.

An abstract class is declared with the keyword “abstract” as shown below.

abstract class Beverages {

	// Defined
	public void addMilk() {

	}

	// abstract
	abstract void addIngredient();
}

Derived/ Subclasses inherit and extend the abstract class, use its defined methods and implement abstract methods for their own.

Below, the Coffee subclass is using the addMilk() method of the Beverages abstract class and have implemented the abstract method addIngredient().

class Coffee extends Beverages {

	void addIngredient() {
		System.out.println("add Coffee");
	}
}

Sample to use the Coffee class:

public class Sample {

	public static void main(String[] args) {

		Coffee b = new Coffee();
		b.addMilk();
		b.addIngredient();
	}
}

“Object of an abstract class in java program cannot be created”

It should be noted that the object of an abstract class cannot be created.

No instance can be created of an abstract class, because simply it is designed to be act as a base class.

Whether it contains a defined method only, or only abstract method or both, instantiation is not allowed.

However, it can be used as a reference variable to store reference of a Subclass as given below.

public class Sample {

	public static void main(String[] args) {
		
		// abstract class reference and subclass object
		Beverages b = new Coffee();
		b.addMilk();
		b.addIngredient();
	}
}

Purpose of an Abstract class in Java Programming

The whole purpose of an abstract class is to have common methods defined in the abstract class and defer/postpone some of its methods to implement to subclasses.

For example, in below code, the Tea and Coffee sub classes are using the common method addMilk() and they are implementing abstract method void addIngredient().

abstract class Beverages {

	public void addMilk() {
		System.out.println("Hot Milk");
	}

	abstract void addIngredient();
}

class Tea extends Beverages {

	@Override
	void addIngredient() {
		System.out.println("add Tea");
	}
}
class Coffee extends Beverages {

	void addIngredient() {
		System.out.println("add Coffee");
	}
}

Repeating,

If you want to provide some already implemented methods to users (subclasses), but in parallel want to force them to implement some methods, you create an abstract class.

Important Points about Abstract Class

An abstract class can extend another class:

An abstract class can extend another class. The Truck abstract class has extended the Vehicle class in the example given below.

class Vehicle{
	public void start(){
		
	}
	public void stop(){
		
	}
}


abstract class Truck extends Vehicle {
	
public abstract void run();

	public void carry() {
		
	}	
}

The class who extends the Truck abstract class will also have access to methods of the Vehicle class.

The following MiniTruck subclass has extended the abstract class Truck, also has access to the Vehicle class methods in addition to the abstract class methods.

class MiniTruck extends Truck {

	@Override
	public void run() {
		
	}	
}

Below is a sample, that demonstrate the methods invocation of the Vehicle, Truck and MiniTruck classes.

public class Sample {

	public static void main(String[] args) {

		MiniTruck mt = new MiniTruck();
		mt.start();
		mt.stop();
		mt.carry();
		mt.run();
	}
}

An abstract class can implement an interface:

An abstract class can also implement an interface in java program as shown below.

interface Chargeable {

	void charge();
}

abstract class Truck implements Chargeable {

	public abstract void run();

	public void carry() {
		
	}	
}

The subclass who extends the abstract class must implement the abstract methods of both the interface and abstract class as well.

class MiniTruck extends Truck {

	@Override
	public void charge() {

	}

	@Override
	public void run() {

	}
}

Also, note that you can override and define (implement) the abstract methods of the interface in the abstract class itself.

In the below example, the abstract class Truck has overridden and defined the method charge () of the Chargeable interface.

interface Chargeable {

	void charge();
}

abstract class Truck implements Chargeable {

	public abstract void run();

	public void carry() {
		
	}

	public void charge() {
		
	}
}

An abstract class can have a constructor:

An abstract class can have a constructor in java program. When we create an object of the subclass, the constructor will be invoked automatically. If the abstract class contains fields, they can be initialized with default values or if we want to perform some operations (methods) before creating an object of the subclass, then it helps.



Leave a Comment