Superclass is the parent class that a subclass (child) inherits from.
It defines common fields and methods that can be reused or overridden in subclasses.
class Animal { // superclass
void speak() {
System.out.println("Animal sound");
}
}
class Dog extends Animal { // subclass
void speak() {
System.out.println("Bark");
}
}
Key Terms
Term | Meaning |
---|---|
Superclass | The base or parent class |
Subclass | The class that extends it |
super | Keyword to refer to superclass |
Accessing Superclass Members
class Animal { // superclass
void speak() {
System.out.println("Animal sound");
}
}
class Cat extends Animal { // subclass
void speak() {
super.speak(); // Calls Animal's speak()
System.out.println("Meow");
}
}
Key Points
- Subclass inherits all non-private members of the superclass.
- Use super.method() to call the superclass version.
- Use super() to call the superclass constructor.
- A class can have only one direct superclass (Java supports single inheritance).
Real-World Analogy
If car is a class, then Vehicle might be its superclass - every car is a vehicle, so shares common traits.
Tip
Be ready to explain:
- the difference between class extension vs interface implementation,
- when to use
super()
vsthis()
,- why Java only supports single class inheritance.
Parent: _OOP