In Java, a superclass is a class from which other classes, called subclasses (or child classes), inherit properties and behaviors. This concept is central to object-oriented programming, specifically the principle of inheritance. The superclass is also known as a parent class or base class.
Note: In Java, every class is based on a fundamental class known as the Object class. This class is part of the java.lang package and is the superclass of all other classes. This means every class in Java automatically inherits features from the Object class.

Using a superclass is helpful because:
A superclass is created like any other Java class. A subclass can inherit from the superclass by using the extends keyword.
The superclass can contain common fields and methods that can be inherited by its subclasses. Each subclass can also define its own fields and methods.
In the following example, the Employee class acts as a superclass for the Developer and Designer classes.
Java
// Superclass
class Employee {
void work() {
System.out.println("The Employee is doing their work.");
}
}
// First Subclass
class Developer extends Employee {
void writeCode() {
System.out.println("The Developer is debugging code.");
}
}
// Second Subclass
class Designer extends Employee {
void createDesign() {
System.out.println("The Designer is creating a design.");
}
}
public class Main {
public static void main(String[] args) {
Developer de1 = new Developer();
de1.work();
de1.writeCode();
Designer des1 = new Designer();
des1.work();
des1.createDesign();
}
}
Output:
The Employee is doing their work. The Developer is debugging code. The Employee is doing their work. The Designer is creating a design.
Explanation
The above Java program demonstrates object-oriented programming using inheritance. The superclass Employee contains a method called work() that prints a message indicating that the employee is doing their work.
The Developer and Designer classes extend the Employee class. Therefore, both subclasses inherit the work() method and can also define their own methods.
The Developer class includes a method called writeCode(), while the Designer class includes a method named createDesign(). In the main() method, objects of both subclasses are created, and each object calls the inherited work() method along with its own method.
This example shows how subclasses can inherit common functionality from a superclass while adding their own specific functionality.
A superclass contains common properties and behaviors that can be inherited by its subclasses. A subclass can use the inherited members and define additional members according to its requirements.
For example, Employee can be a superclass, while Developer and Designer can be subclasses. Both subclasses inherit common employee functionality while providing their own specialized behavior.
A single superclass can be extended by multiple subclasses. Each subclass inherits the accessible members of the superclass and can provide additional functionality.
For example:
Employee
/ \
Developer Designer
Here, Employee is the superclass, while Developer and Designer are subclasses.
In the following example, the Animal class acts as a superclass for the Dog and Cat subclasses, which inherit the eat() method and define their own methods.
Java
class Animal {
void eat() {
System.out.println("The animal is eating.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog is barking.");
}
}
class Cat extends Animal {
void meow() {
System.out.println("The cat is meowing.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
Cat cat = new Cat();
cat.eat();
cat.meow();
}
}
Output:
The animal is eating. The dog is barking. The animal is eating. The cat is meowing.
The extends keyword is used to create an inheritance relationship between a subclass and a superclass.
For example:
class Developer extends Employee {
}
In this example, Developer is the subclass and Employee is the superclass.
In the following example, the Car class extends the Vehicle superclass and inherits its start() method.
Java
class Vehicle {
void start() {
System.out.println("The vehicle is starting.");
}
}
class Car extends Vehicle {
void drive() {
System.out.println("The car is driving.");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start();
car.drive();
}
}
Output:
The vehicle is starting. The car is driving.
Java provides the super keyword to refer to members of the immediate superclass. It can be used to access superclass fields, call superclass methods, and invoke a superclass constructor.
For example:
class Developer extends Employee {
void display() {
super.work();
}
}
Here, super.work() calls the work() method defined in the Employee superclass.
In the following example, the super keyword is used to call a method of the immediate superclass from the subclass.
Java
class Employee {
void work() {
System.out.println("The employee is working.");
}
}
class Developer extends Employee {
void work() {
super.work();
System.out.println("The developer is writing code.");
}
}
public class Main {
public static void main(String[] args) {
Developer developer = new Developer();
developer.work();
}
}
Output:
The employee is working. The developer is writing code.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India