Java ProgramsOOPAbstract Class

Abstract Class in Java

beginner·  OOP  ·  Abstraction

Problem

An abstract class can declare abstract methods with no body at all, alongside ordinary concrete methods with a full implementation — and it can never be instantiated directly, only extended.

Define an abstract Employee class with one abstract method and one concrete method, and provide a subclass that implements the abstract one.

Input
new Developer("Alice")
Output
Alice's salary: 6000.0

Java Program

Java
abstract class Employee { String name; Employee(String name) { this.name = name; } abstract double calculateSalary(); // no body — every subclass must supply one void printDetails() { System.out.println(name + "'s salary: " + calculateSalary()); } } class Developer extends Employee { Developer(String name) { super(name); } double calculateSalary() { return 6000.0; } } public class AbstractClassDemo { public static void main(String[] args) { Employee e = new Developer("Alice"); e.printDetails(); } }

Output

Alice's salary: 6000.0

Core Logic

Leaving calculateSalary() unimplemented in the abstract class, while giving printDetails() a real body that calls it, splits 'what varies per subclass' from 'what every subclass shares'.

How It Works
  1. 1abstract double calculateSalary(); has no body at all — it's a contract, not an implementation.
  2. 2void printDetails() is a full, concrete method, usable by every subclass exactly as written, without needing to be overridden.
  3. 3class Developer extends Employee supplies the missing implementation of calculateSalary(), which is what makes Developer instantiable.
  4. 4Calling printDetails() on a Developer object runs the inherited concrete method, which in turn calls the subclass's own calculateSalary().
new Developer("Alice").printDetails() calls calculateSalary(), which Developer defines as returning 6000.0, printing "Alice's salary: 6000.0".
💡

Key Point: Employee itself can never be instantiated with new Employee(...) — an abstract class exists only to be extended, since it's missing at least one method's actual implementation.

Key Concepts

abstract classabstract methodconcrete method

Related Programs