Skip to main content

Abstract Class Example in Java

3 min read Updated May 29, 2026
Share:
On this page (10sections)

Introduction

Abstract Class is a classic Java console program that demonstrates the concept with complete source code and sample output. Object-oriented programming models real entities with classes, objects, inheritance and polymorphism.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

An abstract type is a type of a nominative type system which cannot be instantiated directly. Abstract types are also known as existential types. An abstract type may provide no implementation or an incomplete implementation. The object-oriented form of abstract types is known as abstract base classes or simply abstract classes. A class extending an abstract class must implement all the abstract methods in the abstract class.

Syntax

abstract class class_name {
	//abstract method_name
}

Abstract Class Example Program

abstract class Employee { 
	String employeeId;
	String employeeName;
	
	Employee(String employeeId,String employeeName){
		this.employeeId = employeeId;
		this.employeeName = employeeName;
	}
	
	void displayName(String employeeName){
		System.out.println("Displaying name from non-abstract method : "+employeeName);
	}

	abstract void role(String employeeId,String employeeName); 
}

class AbstractClassDemo extends Employee{
	String employeeId;
	
	AbstractClassDemo(String employeeId,String employeeName){
		super(employeeId,employeeName);
		this.employeeId = employeeId;
	}
	
	//Overridden abstract method of Employee Class - role
	@Override
	public void role(String employeeId,String employeeName) {
		if(employeeId.equals("ID007")){
			System.out.println(employeeName+" : Captain and Wicket Keeper");
		}else if(employeeId.equals("ID099")){
			System.out.println(employeeName+" : Opening Batsman and Spin Bowler");
		}else{
			System.out.println("Not an Employee");
		}
	}
	
	public static void main(String args[]) { 
	Employee employeeObj = new AbstractClassDemo("ID007","M S Dhoni"); 
	AbstractClassDemo demoObj = new AbstractClassDemo("ID099","Sachin Tendulkar");

	//Implementing non-abstract method (This is not mandatory)
	employeeObj.displayName("M S Dhoni");

	//Implemeting abstract method in class Employee
	employeeObj.role("ID007","M S Dhoni"); 
	demoObj.role("ID099","Sachin Tendulkar"); 
	} 
}

Sample Output

Displaying name from non-abstract method : M S Dhoni
M S Dhoni : Captain and Wicket Keeper
Sachin Tendulkar : Opening Batsman and Spin Bowler

When to use

Use OOP examples when modelling entities with state and behaviour in larger applications.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. this.employeeId = employeeId; updates a variable used in the calculation or output.

  3. this.employeeName = employeeName; updates a variable used in the calculation or output.

  4. A println / print call writes text to the console — part of the sample output below.

  5. this.employeeId = employeeId; updates a variable used in the calculation or output.

  6. The if statement runs the nested code only when the condition is true.

  7. A println / print call writes text to the console — part of the sample output below.

  8. The if statement runs the nested code only when the condition is true.

Best Practices

  • Use meaningful variable and class names that describe their purpose.
  • Compile and run the program locally — modify values to see how output changes.
  • Read compiler errors carefully; they usually point to the exact line to fix.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Abstract Class program demonstrate?
It shows how to implement abstract class in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use OOP examples when modelling entities with state and behaviour in larger applications.

Related Tutorials

Search tutorials