</>Learn to Write Code
LearnPseudocodeReal CodeLanguagesVideosCode TalkAbout
Learn → Programming Fundamentals → Inheritance & Encapsulation

Programming Fundamentals

01Variables02Operators03Input & Output04Sequence05Conditions06Loops07Functions08Objects09Lists10Strings11Errors12Recursion13Inheritance
Lesson 13 of 13·Beginner

Inheritance & Encapsulation

Reuse a blueprint in a more specific one, and hide data so only the blueprint itself can change it.

Related:inheritanceencapsulationextendsprivatepolymorphism

A blueprint can extend another blueprint

A blueprint can EXTEND another blueprint, reusing everything it defines and overriding only the parts that should behave differently.

A blueprint can also mark some of its data PRIVATE, meaning only that blueprint's own actions can read or change it directly. This protects internal data from being changed by mistake from outside code.

  • EXTENDS reuses a blueprint's data and actions in a new, more specific blueprint.
  • The child blueprint can override an action to give it its own behavior.
  • PRIVATE data can only be read or changed from inside the same blueprint.
  • Encapsulation protects internal data from being changed by mistake from outside code.
Think of a general Employee blueprint and a more specific Manager blueprint that extends it, reusing everything an Employee has and adding what makes a Manager different.
Try it: If a Cat blueprint also EXTENDS Animal with its own MakeSound action, what would CALL ACTION myCat.MakeSound show?

Dog extends Animal and overrides MakeSound

Animal hides its energyLevel as PRIVATE data. Dog extends Animal and overrides MakeSound with its own behavior.

Pseudocode
BLUEPRINT Animal
    CREATE name AS ""
    PRIVATE CREATE energyLevel AS 100

    ACTION MakeSound
        SHOW "..."
    END ACTION
END BLUEPRINT

BLUEPRINT Dog EXTENDS Animal
    ACTION MakeSound
        SHOW "Woof!"
    END ACTION
END BLUEPRINT

CREATE Dog AS myDog
CHANGE myDog.name TO "Rex"
CALL ACTION myDog.MakeSound

The same idea in Python

Python inherits with class Dog(Animal), and a double-underscore name hides a field from outside the class.

Python
class Animal:
    def __init__(self):
        self.name = ""
        self.__energyLevel = 100

    def makeSound(self):
        print("...")

class Dog(Animal):
    def makeSound(self):
        print("Woof!")

myDog = Dog()
myDog.name = "Rex"
myDog.makeSound()

The same idea in JavaScript

JavaScript inherits with class Dog extends Animal, and a # prefix declares a private class field.

JavaScript
class Animal {
    #energyLevel = 100;

    constructor() {
        this.name = "";
    }

    makeSound() {
        console.log("...");
    }
}

class Dog extends Animal {
    makeSound() {
        console.log("Woof!");
    }
}

const myDog = new Dog();
myDog.name = "Rex";
myDog.makeSound();

The same idea in C#

C# inherits with class Dog : Animal, marks a field private, and uses virtual/override to replace the inherited behavior.

C#
class Animal
{
    public string Name { get; set; } = "";
    private int EnergyLevel = 100;

    public virtual void MakeSound()
    {
        Console.WriteLine("...");
    }
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Woof!");
    }
}

Dog myDog = new Dog();
myDog.Name = "Rex";
myDog.MakeSound();

The same idea in Java

Java inherits with class Dog extends Animal, marks a field private, and @Override replaces the inherited behavior.

Java
class Animal {
    public String name = "";
    private int energyLevel = 100;

    public void makeSound() {
        System.out.println("...");
    }
}

class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Woof!");
    }
}

Dog myDog = new Dog();
myDog.name = "Rex";
myDog.makeSound();

The same idea in C++

C++ inherits with class Dog : public Animal, keeps a field private, and virtual/override replaces the inherited behavior.

C++
class Animal
{
public:
    std::string name = "";

    virtual void MakeSound()
    {
        std::cout << "...\n";
    }

private:
    int energyLevel = 100;
};

class Dog : public Animal
{
public:
    void MakeSound() override
    {
        std::cout << "Woof!\n";
    }
};

Dog myDog;
myDog.name = "Rex";
myDog.MakeSound();
Lesson 13 of 13 · Concept
← Previous Lesson: RecursionEnd of fundamentals

Learn to Write Code

Learn the idea first, write it in pseudocode, then see how real programming languages express the same logic.

Learn

CurriculumPseudocode StandardLanguagesReal Code

Elsewhere

Code TalkAbout BrianSpread the WordCoders Rock — PracticecodeBetter on YouTubeSupporting files on GitHubAdvertising & Promotions
© 2026 Brian Kittrell, an exteran studios site. Educational content and examples are provided for learning purposes.