Superclass and Subclass in Java Example

We have known in the previous tutorial that inheritance is a technique in which a new class can be created 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.

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 some key points about superclass and subclass in Java in 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 the 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 observe in this program, the subclass members cannot be accessed by using the superclass object reference variable.

But if we create an object of the subclass, all the members of both superclass and subclass can be accessed.

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

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

As you can see in the above program, we easily accessed members of the superclass as well as subclass creating an object of subclass.
Therefore, we always create an object of subclass in inheritance.

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 write a program where the names of instance variables and methods in superclass and subclass will be the same and see that can we access them or not?

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 program, 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 superclass members from the subclass. It can be applied with superclass variables, methods, and constructors.

Let’s take the above example program where we will access superclass 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 superclass members from subclass by using the super keyword.

Hope that this tutorial has covered all the important points and example program related to superclass and subclass in java. I hope that you will have understood the basic points of base class and derived class.
Thanks for reading!!!Next ⇒ Behavior of access specifiers in case of Java Inheritance

⇐ PrevNext ⇒

Leave a Comment