What is base class and subclass?

In the world of object-oriented programming languages, both base and derived classes play an important role. The base class is the existing class whereas the derived class is one that acquires the properties of a base class. There are many differences between them, let’s explore some major differences between a base class and a derived class.

What is a Base Class?

In an object-oriented programming language, a base class is an existing class from which the other classes are determined and properties are inherited. It is also known as a superclass or parent class. In general, the class which acquires the base class can hold all its members and some further data as well.

Syntax: Class base_classname{ … }.

What is a Derived Class?

A derived class is a class that is constructed from a base class or an existing class. It has a tendency to acquire all the methods and properties of a base class. It is also known as a subclass or child class.

Syntax: Class derived_classname : access_mode base_class_name { … }.

Difference between Base Class and Derived Class in C++

S.No.Base ClassDerived Class1.A base class is an existing class from which the other classes are derived and inherit the methods and properties.A derived class is a class that is constructed from a base class or an existing class.2.Base class can’t acquire the methods and properties of the derived class.Derived class can acquire the methods and properties of the base class.3.The base class is also called superclass or parent class.The derived class is also called a subclass or child class.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus, GATE Previous Year Question Paper, and more.

Inheritance is the act of deriving a new class from an existing class. Inheritance allows us to extend the functionality of the object. The new class automatically contains some or all methods and variables of the original class. The new derived class is called a child class, or subclass. The original class is called parent class, or superclass, or base class. Since the derived class is always more specific, the relation between these classes is called is-a relation. Class extension can be used for a number of purposes. The most common use is specialization - where the extended class defines new behaviors and thus becomes a specialized version of its superclass. Consider two classes Book and Dictionary. Which one is more general? Book, since a dictionary is a specific kind of a book. Therefore, we say that Dictionary extends Book.

public class Dictionary extends Book
{
} 

All public methods declared in Book available in Dictionary, but not vice versa: inheritance is a one-way street. Inheritance allows the following three changes in derived class:

  1. add new fields (variables)
  2. add new methods
  3. override existing (in base class) methods

The following two changes are NOT allowed:

  1. derived class cannot remove fields (variables)
  2. derived class cannot remove methods.

There are two forms of inheritance:

  1. inheritance of type: whereby the subclass acquires the type of the base class;
  2. inheritance of implementation: whereby the subclass acquires the implementation of the base class;

These two forms always occur together.

Implementation by Inheritance

The following picture demonstrates the stack implementation by inheritance

See Stack.java for a complete implementation of the Stack class.

super

Constructors are not inherited in a derived class. Therefore, a child's constructor is responsible for calling the parent's constructor:
public Stack()
{
   super();
}

By keyword super Java provides a mechanism to call parent's constructor. The above super() is actually a call for ArrayList's constructor. The super must be used in the first line (literally) of a constructor.

If the child class constructor does not call super, the parent's constructor with no arguments will be implicitly called. If parent class implements a constructor with arguments and has no a constructor with no arguments, then the child constructors must explicitly call a parents constructor.

The super reference can also be used to invoke other methods in parent's class. How would you call the ArrayList's toString method from inside the Stack's toString?. Since both methods have the same signature, we say that Stacks's toString overrides Book's toString(). We will distinguish between them using

public Stack()
{
   super();
}
1 and super keywords. Consider the following code example and trace
public Stack()
{
   super();
}
3 call:

public class Demo
{
   public static void main(String[] args)
   {
      System.out.println( new B().m1()  );
   }
}

class A
{
   int m1() {return m3();}
   int m2() {return this.m3();}
   int m3() {return 1;}
}

class B extends A
{
   int m1() {return super.m1();}
   int m3() {return 4;}
   int m4() {return m2();}
}

Here is a trace:

public Stack()
{
   super();
}
4.

A child class can override any public parent's method that is not defined with the

public Stack()
{
   super();
}
5 modifier. Also, classes with
public Stack()
{
   super();
}
5 modifier cannot be extended. The Java's String and StringBuffer classes are examples of such classes.

protected

The derived class inherits all variables and methods, which are public. It does not inherit those variables and methods, which are private. However, we do not want to declare all variables and methods to be public, since they will be visible for everybody. Therefore, Java offers another modifier called

public Stack()
{
   super();
}
7. Protected variables are not accessible for other classes, but only for derived classes.

Hierarchies

Multiple classes can be derived from a single parent. There is no limit to the number of children a class can have (but a child can have only one parent). Two children of the same parent are called siblings. Siblings are NOT related to each other by inheritance. Inheritance is transitive. All methods and fields are passed from a parent to the children and then from children to their children and further. There is no single best hierarchy, the decision is made when you design your classes.

Polymorphism

The term polymorphism can be defined as having many forms. A polymorphic reference is one that refer to different types of the objects. Polymorphism allows programs to be written more abstractly and therefore less redundantly. Polymorphism is implemented in Java using a technique called dynamic binding - where exact types are determined during the program execution.

Consider a collection of greeting cards: the base class is Card which has a subclass Holiday which has a subclass ApprilFool:

What is a base class?

What is a Base Class? In an object-oriented programming language, a base class is an existing class from which the other classes are determined and properties are inherited. It is also known as a superclass or parent class.

What is the relationship between a Baseclass and subclass?

A specialized class inherits from a general class. A subclass inherits from a superclass. A child class inherits from a parent class. A derived class inherits from a base class.

What is difference between subclass and class?

Definition: A subclass is a class that derives from another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a class's direct ancestor as well as all of its ascendant classes.

What is a subclass with example?

A subclass is a class derived from the superclass. It inherits the properties of the superclass and also contains attributes of its own. An example is: Car, Truck and Motorcycle are all subclasses of the superclass Vehicle.