Lectern

Search

Search IconIcon to open search

Inheritance

Last updated Nov 21, 2022

A feature that allows the derivation of a new class from an existing class. Introduces a way to reuse existing code without duplicating them.

A subclass inherits all the functionality of a superclass, but can also have its own unique attributes and methods.

# Terminologies

TermDefinition
Superclass (also parent or base class)The class from which subclasses can be created from
Subclass (also child or derived class)The class that inherits a superclass

# Declaration

In many OOP-based languages, a subclass can be created using the : symbol as such:

1
2
3
4
5
6
// CashCard is an existing class that is the superclass.
// MemberCashCard is the new class deriving from CashCard.
class MemberCashCard: CashCard
{
	// Unique attributes, properties, and methods
}
1
2
3
4
# CashCard is an existing class that is the superclass.
# MemberCashCard is the new class deriving from CashCard.
class MemberCashCard(CashCard):
	# Unique attributes, properties, and methods

# Inheriting superclass

A subclass can call upon the superclasses’s attributes and properties, though the exact method differs across programming languages.

Some examples when inheriting superclass is used include:

1
2
3
4
5
// Defining the constructor for MySubClass
public MySubClass(subparams) : base(superparams)
{
	// other initialization
}