Superclass and Subclass in Java with Example

In this tutorial, we will understand the concepts of superclass and subclass in Java with the help of various examples.

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. That is, 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.

A class that is used to create a new class is called superclass. In the words, the class from where a subclass inherits the features is called superclass. It is also called a base class or parent class.

A class that inherits all the members (fields, method, and nested classes) from the other class is called subclass. In simple words, a newly created class is called subclass. It is also called a derived class, child class, or extended class.

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.

Superclass and Subclass Examples


If we create an object of the superclass, we can access only the members of the superclass. We cannot access the subclass members by creating an object of superclass.

Let’s take an example program where we will create an object of superclass and see that can we access subclass members using superclass reference?

Program code 1:

package inheritance;
public class Parentclass 
{
  void m1()
  { 
     System.out.println("Superclass m1 method"); 
  } 
 } 
public 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(); 
 // Here, subclass members are not available to superclass. 
 // So, we cannot access them using superclass reference variable.
 } 
}
Output: 
      Superclass m1 method

As you can observe in this example, the subclass members cannot be accessed by using the superclass object reference variable. But if we create an object of the subclass, we can access all the members of both superclass and subclass.


Let’s take an example program where we will access both superclass and subclass members by creating an object of the baseclass.

Program code 2:

package inheritance; 
public class Parentclass 
{ 
  void m1()
  { 
    System.out.println("Superclass m1 method"); 
  } 
} 
public 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(); 
    c.m2(); 
   } 
 }
Output: 
       Superclass m1 method 
       Childclass m2 method

As you can see in the above program, 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.


Program code 3:

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.");
    }
    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.

The Dog class also has an additional method wagTail() that is specific to dogs. In the Main class, we have created instances of both “Animal” and “Dog” classes and call their respective methods.

Program code 4:

package inheritance;
// Base class
class Vehicle 
{
   String brand;
   Vehicle(String brand) {
      this.brand = brand;
   }
   void honk() {
      System.out.println("Honk honk!");
   }
}
// Subclass inheriting 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 superclass and subclass members have the same name. Can we access members of superclass in this case?

In this case, by default, only the members of subclass are accessible.

Let’s take an example program in which the names of instance variables and methods in superclass and subclass will be the same and see that we can access them or not?

Program code 3:

package inheritance; 
public class Number 
{ 
   int x = 20; 
   void display() 
   { 
     System.out.println("X = " +x); 
   } 
} 
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 20 is displayed. We could access only subclass members. So, here the question is how to access superclass members from the subclass?

We 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 method and instance variable by using a super keyword from the subclass.

Program code 4:

package inheritance; 
public class Number 
{ 
   int x = 20; 
   void display() 
   { 
      System.out.println("X = " +x); 
   } 
} 
public class Number2 extends Number 
{ 
   int x = 50; 
   void display() 
   { 
      System.out.println("X = " +super.x); // Accessing superclass instance variable using super keyword. 
      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, we can access superclass members from the subclass by using the super keyword.


In this tutorial, we have explained superclass and subclass in Java with the help of some important examples. Hope that you will have understood the basic concepts of parent class and child class. In the next, we will understand the behavior of access modifiers in case of inheritance.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love