Superclass and Subclass in Java
In this tutorial, we will understand the concepts of superclass and subclass in Java with the help of various examples.
What is Inheritance in Java?
Inheritance is one of the most powerful features of object-oriented programming (OOP) in Java. It is a technique which allows us to create a new class by extending a previously declared class. We can create a new class from the existing class.
By using the inheritance concept, we can acquire all the features (members) of a class and use them in another class by relating the objects of two classes.
What is Superclass and Subclass in Java?
A class that is used to create a new class is called superclass in Java. In other words, the class from where a subclass inherits the features is called superclass. It is also called a base class or parent class in Java.
A class that inherits all the members (fields, method, and nested classes) from the other class is called subclass in Java. In simple words, a newly created class is called subclass. It is also called a derived class, child class, or extended class in Java.
Thus, the process of creating a subclass from a superclass is called inheritance in Java. Let’s take some important example programs based on superclass and subclass in Java that will help to you to clear concepts of inheritance.
Java Superclass and Subclass Examples
Let’s take some important example programs based on accessing members of superclass and subclass in Java.
Example 1: Superclass Object – Accessing Members of Superclass
If you create an object of the superclass, you can access only the members defined in the superclass. You cannot access the subclass members by creating an object of superclass. Let’s take an example where we will create an object of superclass and see that can we access subclass members using superclass reference?
package inheritance;
class Parentclass
{
void m1()
{
System.out.println("Superclass m1 method");
}
}
class Childclass extends Parentclass
{
void m2()
{
System.out.println("Childclass m2 method");
}
}
public class Test {
public static void main(String[] args)
{
// Creating an object of superclass.
Parentclass p = new Parentclass();
p.m1(); // Allowed
// Here, subclass members are not available to superclass.
// So, we cannot access them using superclass reference variable.
// p.m2(); // Not allowed
}
}Output:
Superclass m1 method
As you can observe in this example, we can access only the members defined in the superclass. We cannot access members of subclass using the superclass object reference variable. This is because the reference type (Parentclass) decides which members are accessible.
Java checks the reference type at compile time to determine which fields and methods are accessible. The superclass has no knowledge of subclass-specific methods or fields. Inheritance works top-down, not bottom-up.
Example 2: Subclass Object – Access Both Superclass and Subclass Members
If you create an object of the subclass, you can access all the members of both superclass and subclass. This is because the subclass inherits all accessible (non-private) members of the superclass. Let us consider an example where we will access both superclass and subclass members by creating an object of the subclass.
package inheritance;
class Parentclass
{
void m1()
{
System.out.println("Superclass m1 method");
}
}
class Childclass extends Parentclass
{
void m2()
{
System.out.println("Childclass m2 method");
}
}
public class Test {
public static void main(String[] args)
{
// Creating an object of subclass.
Childclass c = new Childclass();
// Accessing superclass and subclass members using subclass object reference variable.
c.m1(); // Inherited from Parentclass
c.m2(); // Defined in Childclass
}
}Output:
Superclass m1 method Childclass m2 method
As you can see in this example, we have easily accessed members of the superclass as well as subclass by creating an object of subclass. Therefore, we always create an object of subclass in inheritance.
Example 3: Method Overriding in Java Using Superclass and Subclass
package inheritance;
class Animal {
// Overriding method.
void makeSound() {
System.out.println("Animal makes a sound.");
}
}
class Dog extends Animal {
// Overridden method.
void makeSound() {
System.out.println("Dog barks.");
}
// Subclass specific method
void wagTail() {
System.out.println("Dog wags its tail.");
}
}
public class Main {
public static void main(String[] args)
{
Animal animal = new Animal();
Dog dog = new Dog();
animal.makeSound();
dog.makeSound();
dog.wagTail();
}
}
Output:
Animal makes a sound. Dog barks. Dog wags its tail.
In this example, we have created a superclass “Animal” with a method makeSound(). We then have created a subclass “Dog” that extends the Animal class and overrides the makeSound() method to provide its own implementation. Here,
- Method signature (name + parameters) is the same.
- Access level is not reduced.
- Return type is compatible.
So, this follows Java method overriding rules.
The Dog class also has an additional method wagTail() that is specific to dogs. The wagTail() method exists only in the Dog class. This represents specialized behavior, which is a key concept of inheritance.
In the Main class, we have created instances of both “Animal” and “Dog” classes and call their respective methods.
- animal.makeSound() → calls Animal’s implementation
- dog.makeSound() → calls Dog’s overridden implementation
- dog.wagTail() → calls Dog-specific method
Example 4: Inheritance with Multiple Subclasses
package inheritance;
// Base class
class Vehicle
{
String brand;
Vehicle(String brand) {
this.brand = brand;
}
void honk() {
System.out.println("Honk honk!");
}
}
// Subclass inheriting members from Vehicle class.
class Car extends Vehicle
{
String model;
Car(String brand, String model) {
super(brand);
this.model = model;
}
void drive() {
System.out.println("Driving the " + brand + " " + model);
}
}
// Subclass inheriting from Vehicle class.
class Bicycle extends Vehicle
{
Bicycle(String brand) {
super(brand);
}
void pedal() {
System.out.println("Pedaling the " + brand + " bicycle");
}
}
public class Main {
public static void main(String[] args)
{
Car myCar = new Car("Toyota", "Corolla");
Bicycle myBicycle = new Bicycle("Schwinn");
myCar.drive();
myCar.honk();
System.out.println();
myBicycle.pedal();
myBicycle.honk();
}
}
Output:
Driving the Toyota Corolla Honk honk! Pedaling the Schwinn bicycle Honk honk!
In this example, we have created a base class Vehicle with a brand attribute and a honk() method. The Car subclass inherits from Vehicle and adds a model attribute and a drive() method. The Bicycle subclass also inherits from Vehicle and adds a pedal() method.
In the Main class, we have created instances of both Car and Bicycle and showcase how inheritance allows the subclasses to access the attributes and methods of the superclass. This exhibits the reusability and extensibility features of inheritance in object-oriented programming.
Base Class and Derived Class Members with Same Name
Suppose that a superclass and a subclass members (variables or methods) have the same name. In this case:
- Subclass members hide superclass variables.
- Subclass methods override superclass methods.
By default, only the members of subclass are accessible. This is because Java gives priority to the subclass members.
Let’s take an example in which the names of instance variable defined in both superclass and subclass will be the same and see that we can access them or not?
Example 5: Accessing Superclass Variables
package inheritance;
public class Number
{
int x = 20;
}
public class Number2 extends Number
{
int x = 50;
void display()
{
System.out.println("X = " +x);
}
}
public class NumberTest {
public static void main(String[] args)
{
// Creating an instance of subclass.
Number2 n = new Number2();
n.display();
}
}Output:
X = 50
In the preceding example, the subclass instance variable x with value 50 is displayed because the subclass variable hides the superclass variable. We could access only subclass variable. So, here the question is how to access superclass members from the subclass?
Accessing Superclass Members from Subclass
If the superclass and subclass members (variables or methods) have the same name, you can access superclass members from the subclass using the keyword “super“. The keyword “super” refers to superclass members from the subclass. We can apply it with superclass variables, methods, and constructors.
Let’s take the above example where we will access the superclass variable by using super keyword from the subclass.
Example 6: Accessing Superclass Variable Using super Keyword
package inheritance;
public class Number
{
int x = 20;
}
public class Number2 extends Number
{
int x = 50;
void display()
{
// Accessing superclass variable using super keyword.
System.out.println("X = " +super.x);
System.out.println("X = " +x);
}
}
public class NumberTest {
public static void main(String[] args)
{
Number2 n = new Number2();
n.display();
}
}Output:
X = 20 X = 50
Thus, when superclass and subclass members have the same name in any Java program, you can access superclass members from the subclass by using the super keyword.
Example 7: Accessing Superclass Method Using super Keyword
package inheritance;
class Parent {
void show() {
System.out.println("Parent show");
}
}
class Child extends Parent {
void show() {
super.show(); // Calls Parent's method
System.out.println("Child show");
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
obj.show();
}
}
Output:
Parent show Child show
In this example, the Child class overrides the show() method of the Parent class. Inside the overridden method, super.show() is used to explicitly call the superclass method. This example program demonstrates method overriding and the use of the super keyword clearly and correctly.
Conclusion
In Java, the concepts of superclass and subclass form the foundation of inheritance, which is one of the core principles of object-oriented programming. A superclass represents a generalized class that contains common properties and behaviors, while a subclass is a specialized class that inherits these features and can add new functionality. The subclass can also modify existing behavior through method overriding.
By using inheritance, Java promotes code reusability, extensibility, and better program organization. We hope that you will have understood the concepts of superclass and subclass in Java and practiced all example programs.








