Superclass and Subclass in Java Example

In the previous tutorial, we learned inheritance concept in java. Basically, inheritance is a technique in which a new class can be created from the existing class. 

By using 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.

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. It is also called a derived class, child class or extended class.

Let’s understand the superclass and subclass some more detail in this chapter. 

Superclass and Subclass in Java


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

Let’s create a program where we will create an object of superclass and see that can we access subclass members using superclass reference?
Program source 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 see in the above program, the subclass members cannot be accessed by using superclass object reference variable. But if we create an object of subclass, all the members of both superclass and subclass can be accessed. Let’s see in the example program.


Program source 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 superclass. 
     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

Therefore, we always create an object of subclass inheritance.

Suppose that superclass and subclass members have the same name then in this case, by default, only the members of subclass are accessible.

Let’s write a program where the names of instance variables and methods in superclass and subclass are the same.
Program source 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) 
  { 
    Number2 n = new Number2(); 
     n.display(); 
   } 
 }
Output: 
       X = 50

In the preceding example, the subclass instance variable x value 20 is displayed. We could access only subclass members. So, here the question is how to access superclass members from the sub class?

We can access superclass members from subclass using the keyword “super“. The keyword “super” refers to super class members from sub class. It can be applied with super class variables, methods, and constructors.

Let’s take the above example program where we will access super class method and instance variable by using a super keyword from the sub class.
Program source 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 super class members from sub class by using super keyword.

For more example programs based on superclass constructor, variables and methods, go to the below link:
👉 Super keyword in Java | Use, Example Program

Final words
Hope that this tutorial has covered all the important points and example program related to super class and sub class in java. I hope that you will have understood this topic.
Thanks for reading!!!
Next ⇒ Behavior of access specifiers in case of Java Inheritance

⇐ PrevNext ⇒

Leave a Comment