Super and Sub Classes

A superclass (also called a base or parent class) is the general class being inherited from, and a subclass (also called a derived or child class) is the more specific class that inherits from it. The subclass gets everything the superclass has and can extend or override it, while the superclass knows nothing about its subclasses.

2 min read

Superclass and subclass are just the formal names for the two sides of an inheritance relationship. The superclass (also called the base class or parent class) is the more general class. The subclass (also called the derived class or child class) is the more specific one that inherits from it.

How it works

A subclass automatically has everything the superclass has, plus whatever it adds on top. The relationship only goes one direction: a superclass has no idea what subclasses exist or what they've added, but every subclass knows exactly what it inherited.

Keywords for expressing this differ by language: C++ uses class Sub : public Super, Java and C# use class Sub extends Super, Python uses class Sub(Super):.

A worked example

class Animal {          // superclass
    String name;
    void eat() {
        System.out.println(name + " is eating.");
    }
}

class Dog extends Animal {   // subclass
    void bark() {
        System.out.println(name + " says woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.name = "Rex";
        myDog.eat();    // inherited from Animal
        myDog.bark();   // defined in Dog
    }
}

Dog is the subclass here, Animal is the superclass. Dog gets name and eat() for free, and adds bark(), which Animal knows nothing about and could never call.

Where this matters in real systems

Exception hierarchies in almost every mainstream language are built exactly this way. In Java, Exception is a broad superclass, and specific problems like FileNotFoundException or NullPointerException are subclasses of it (through intermediate classes). This lets code catch a very specific subclass when it wants precise handling, or catch the general superclass when it just wants to handle "anything that went wrong" in one place.

What interviewers actually ask about superclasses and subclasses

  • "Can a subclass have multiple superclasses?" Depends on the language: C++ allows multiple inheritance directly; Java and C# only allow single inheritance from a class, though a class can implement multiple interfaces.
  • "Does a subclass inherit private fields?" The fields exist in memory as part of the object, but the subclass cannot access them directly by name, only through public or protected methods the superclass provides.
  • "Can you have an instance of just the superclass?" Yes, unless the superclass is explicitly marked abstract, in which case it can only be used through its subclasses.
  • "If a subclass overrides a method, can it still call the superclass's version?" Yes, using super.methodName() (Java/C#) or the explicit Base::methodName() syntax in C++.

Key takeaways

  • The superclass is the general, shared class; the subclass is the specific class that inherits from it.
  • A subclass gets everything from the superclass automatically, plus whatever it adds.
  • The superclass has no awareness of its subclasses or what they add.
  • Exception hierarchies are a common real-world example of superclass/subclass design.
All 72 OOP interview questions

Log in to save which topics you've read across every subject.

Frequently asked questions

Can a subclass have multiple superclasses?

Depends on the language: C++ allows multiple inheritance directly; Java and C# only allow single inheritance from a class, though a class can implement multiple interfaces.

Does a subclass inherit private fields?

The fields exist in memory as part of the object, but the subclass cannot access them directly by name, only through public or protected methods the superclass provides.

Can you have an instance of just the superclass?

Yes, unless the superclass is explicitly marked abstract, in which case it can only be used through its subclasses.

If a subclass overrides a method, can it still call the superclass's version?

Yes, using `super.methodName()` (Java/C#) or the explicit `Base::methodName()` syntax in C++.