Compare the Difference Between Similar Terms

Difference Between Superclass and Subclass

Key Difference – Superclass vs Subclass
 

In Object Oriented Programming (OOP), the system is modelled using objects. These objects are created using a class. A class is a blueprint or a description to create an object. Object creation is also known as object instantiation. Each object is communicating with other objects. A program or software can be developed using Object Oriented Programming. Inheritance is a major concept in OOP. It improves code reusability. Rather than implement a program from the beginning, it allows inheriting properties and methods of the already existing class to a new class. It helps to make the program more manageable. Superclass and Subclass are two terms that are related to inheritance. This article discusses the difference between Superclass and Subclass. The key difference between the Superclass and Subclass is that Superclass is the existing class from which the new classes are derived while Subclass is the new class that inherits the properties and methods of the Superclass.

CONTENTS

1. Overview and Key Difference
2. What is Superclass
3. What is Subclass
4. Similarities Between Superclass and Subclass
5. Side by Side Comparison – Superclass vs Subclass in Tabular Form
6. Summary

What is Superclass?

In Inheritance, the existing class from which the new classes are derived is known as the Superclass. It is also known as the parent class or base class.

There are different inheritance types. There are illustrated using the following examples. Consider A B and C as classes.

Figure 01: Inheritance Types

Figure 02: Hybrid Inheritance

According to the above diagrams, Superclasses varies from each inheritance type. In single-level inheritance, A is the Superclass. In Multilevel inheritance, A is the Superclass for B and B is the Superclass for C. In Hierarchical Inheritance A is the Superclass for both B and C. In multiple inheritances both A and B are Superclasses for C.

Hybrid inheritance is a combination of multi-level and multiple inheritances. In the left-hand side diagram, A is the Superclass for B, C and B, C are the Superclasses for D. In the right-hand side diagram, A is the Superclass for B and B, D are Superclasses for C.

 Refer the below program written in Java.

Figure 03: Inheritance Program in Java

According to the above program, class A have sum() and sub() methods. Class B has multiply() method. Class B is extending class A. Therefore, properties and methods of class A are accessible by class B. Therefore, class A is the Superclass. The reference type of class B is taken to create the object. So, all methods such as sum(), sub() and multiply() are accessible by the object. If Superclass reference type is used for object creation, the members of class B cannot be accessible. e.g. A  obj = new B(); Therefore, Superclass reference cannot call the method multiply() because that method belongs to class B.

What is Subclass?

According to the above diagrams, Subclasses varies from each inheritance type. In Single Inheritance, B is the Subclass. In multi-level inheritance, B is the Subclass of A and C is the Subclass of B. In Hierarchical Inheritance B and C are Subclasses of A. In multiple inheritances, C is the Subclass for A and B.

In Hybrid inheritance, the diagram in the left, B and C are Subclasses of A. D is the Subclass of B and C. In the diagram to the right, B is the Subclass for A. C is the Subclass of B and D.

According to the above Inheritance program, class B is extending class A. Therefore, all properties and methods of class A are accessible by class B. Class B is the new class that inherits from class A. It is known as the Subclass. It is also known as the child class or derived class. Class B has multiply () method and it can also access sum() and sub() methods of class A using inheritance.

What is the Similarity Between Superclass and Subclass?

What is the Difference Between Superclass and Subclass?

Superclass vs Subclass

When implementing inheritance, the existing class from which the new classes are derived is the Superclass. When implementing inheritance, the class that inherits the properties and methods from the Superclass is the Subclass.
 Synonyms
Superclass is known as base class, parent class. Subclass is known as derived class, child class.
Functionality
A superclass cannot use the properties and methods of the Subclass. A subclass can use the properties and methods of the Superclass.
 Single-Level-Inheritance
There is one Superclass. There is one Subclass.
Hierarchical Inheritance
There is one Superclass There are many Subclasses.
Multiple Inheritance
There are many Superclasses. There is one Subclass.

Summary – Superclass vs Subclass 

Inheritance is a concept of OOP. It allows using properties and methods of an existing class to be accessed by a new class. The inherited class is the Superclass, and derived class is the Subclass. The difference between the Superclass and Subclass is that Superclass is the existing class from which new classes are derived while Subclass is the new class that inherits the properties and methods of the Superclass.

Download the PDF Superclass vs Subclass

You can download PDF version of this article and use it for offline purposes as per citation note. Please download PDF version here Difference Between Superclass and Subclass

Reference:

1.“Inheritance in Java – Javatpoint.” The Point, Available here 
2.tutorialspoint.com. “Java Inheritance.” The Point, Available here