Inheritance
Learn how inheritance lets one object reuse or extend behavior from another in BornomalaScript.
Inheritance
Inheritance lets one object or blueprint reuse behavior from another.
That is useful when two things share the same core behavior but one of them needs extra features.
Why Inheritance Matters
Inheritance helps you avoid repeating code.
Instead of rewriting the same behavior again and again, you can define a base version and extend it where needed.
Simple Example
Imagine a base object for a person and a more specific object for a student.
The student can reuse the person’s common data and add school-specific details.
Conceptual Example
dhoro Person = {
nam: "",
boltePare: kaj() {
lekho("Ami ekjon manush")
}
}
dhoro Student = Person
Student.className = ""
This is a simplified example to show the idea of reusing behavior.
How It Works
Inheritance usually gives you:
- a parent type
- a child type
- shared behavior in the parent
- extra behavior in the child
That means you can build more specific things without starting from zero each time.
Step-by-Step Breakdown
- Create a base object or blueprint
- Put common behavior there
- Create a more specific version
- Add or change only the behavior that is different
Common Mistakes
Beginners often make these mistakes:
- using inheritance when simple composition would be better
- adding too much into the base type
- forgetting that the child should only extend what it needs
Practice Task
Try designing inheritance for:
- Vehicle and Car
- Person and Teacher
- Device and Phone
Think about what should stay shared and what should be unique.
Quick Checklist
Before moving on, make sure you can:
- explain why inheritance is useful
- identify a shared parent concept
- describe how a child type can extend it
If yes, you are ready for polymorphism.