The Power of Inheritance: Sharing Properties Like a Pro

By Sylvester Das
•November 24, 2025
•5 min read
In the world of programming, we often deal with objects and their properties. But what happens when you want to share a property value between a parent object and its children? That's where inheritance comes in handy! Inheritance allows a child object to automatically inherit properties and methods from its parent, promoting code reuse and reducing redundancy. This article explores how property inheritance works, providing practical examples and technical insights to help you master this powerful concept.
What is Property Inheritance?
Imagine a family where children inherit traits from their parents. In programming, property inheritance works similarly. A "child" object (also known as a subclass or derived class) inherits properties and methods from its "parent" object (also known as a superclass or base class). This means the child automatically possesses certain characteristics defined in the parent, without needing to explicitly define them again.
This is super useful for organizing your code and avoiding repetition. If several objects share common properties, you can define those properties in a parent class and let the child classes inherit them. This makes your code more maintainable and easier to understand.
Inheritance in Action: A JavaScript Example
Let's look at a practical example using JavaScript. We'll create a Vehicle class with common properties like color and speed, and then create child classes like Car and Bicycle that inherit from Vehicle.
// Parent class: Vehicle
class Vehicle {
constructor(color, speed) {
this.color = color;
this.speed = speed;
}
describe() {
return `This vehicle is ${this.color} and can travel at a speed of ${this.speed} km/h.`;
}
}
// Child class: Car
class Car extends Vehicle {
constructor(color, speed, numberOfDoors) {
super(color, speed); // Call the parent's constructor
this.numberOfDoors = numberOfDoors;
}
describe() {
return `${super.describe()} It has ${this.numberOfDoors} doors.`;
}
}
// Child class: Bicycle
class Bicycle extends Vehicle {
constructor(color, speed, hasBasket) {
super(color, speed); // Call the parent's constructor
this.hasBasket = hasBasket;
}
describe() {
return `${super.describe()} It has a basket: ${this.hasBasket}.`;
}
}
// Creating instances
const myCar = new Car("red", 180, 4);
const myBicycle = new Bicycle("blue", 25, true);
console.log(myCar.describe()); // Output: This vehicle is red and can travel at a speed of 180 km/h. It has 4 doors.
console.log(myBicycle.describe()); // Output: This vehicle is blue and can travel at a speed of 25 km/h. It has a basket: true.
Explanation:
VehicleClass: This is our parent class, defining the basic properties of a vehicle:colorandspeed. Thedescribe()method provides a general description of the vehicle.CarandBicycleClasses: These are child classes that inherit fromVehicle. Theextendskeyword indicates the inheritance relationship.super(): Inside the child class constructors, we usesuper(color, speed)to call the parent's constructor. This initializes the inherited properties (colorandspeed) from theVehicleclass. It's crucial to callsuper()before accessingthisin the child constructor.Adding Unique Properties: The child classes add their own unique properties:
numberOfDoorsforCarandhasBasketforBicycle.Overriding Methods: The child classes also override the
describe()method to provide a more specific description. They usesuper.describe()to call the parent'sdescribe()method and then add their own details.
Technical Deep-Dive: The Prototype Chain
In JavaScript, inheritance is implemented using prototypes. Every object has a prototype, which is another object that it inherits properties and methods from. When you try to access a property on an object, JavaScript first checks if the object itself has that property. If not, it looks at the object's prototype, and then the prototype's prototype, and so on, until it finds the property or reaches the end of the prototype chain (which is null).
When you use extends in a class, JavaScript sets up the prototype chain so that the child class's prototype points to the parent class. This is how inheritance is achieved under the hood.
Practical Implications and Benefits
Using inheritance offers several key benefits:
Code Reusability: Avoid writing the same code multiple times. Inherit common properties and methods from a parent class.
Maintainability: Changes to the parent class automatically propagate to all child classes, making it easier to maintain your code.
Organization: Inheritance helps to organize your code into a hierarchy of related objects, improving readability and understanding.
Extensibility: Easily add new classes that inherit from existing ones, extending the functionality of your application.
When to Use Inheritance (and When Not To)
Inheritance is a powerful tool, but it's not always the best solution. Consider using inheritance when:
You have a clear "is-a" relationship between objects (e.g., a Car is a Vehicle).
You want to share common properties and methods between multiple classes.
You want to create a hierarchy of related objects.
However, avoid overusing inheritance, as it can lead to complex and rigid class hierarchies. In some cases, composition (where objects contain other objects as properties) might be a better alternative. Consider composition when:
You want to reuse code without creating a strict "is-a" relationship.
You want to create more flexible and loosely coupled objects.
You want to avoid the complexities of deep inheritance hierarchies.
Inheritance in Python
Let's create the same example in Python:
# Parent class: Vehicle
class Vehicle:
def __init__(self, color, speed):
self.color = color
self.speed = speed
def describe(self):
return f"This vehicle is {self.color} and can travel at a speed of {self.speed} km/h."
# Child class: Car
class Car(Vehicle):
def __init__(self, color, speed, number_of_doors):
super().__init__(color, speed) # Call the parent's constructor
self.number_of_doors = number_of_doors
def describe(self):
return f"{super().describe()} It has {self.number_of_doors} doors."
# Child class: Bicycle
class Bicycle(Vehicle):
def __init__(self, color, speed, has_basket):
super().__init__(color, speed) # Call the parent's constructor
self.has_basket = has_basket
def describe(self):
return f"{super().describe()} It has a basket: {self.has_basket}."
# Creating instances
my_car = Car("red", 180, 4)
my_bicycle = Bicycle("blue", 25, True)
print(my_car.describe())
print(my_bicycle.describe())
The concept is the same, just implemented in Python's syntax.
Conclusion
Property inheritance is a fundamental concept in object-oriented programming that allows you to share properties and methods between parent and child objects. By understanding how inheritance works, you can write more efficient, maintainable, and organized code. Remember to use inheritance judiciously and consider alternative approaches like composition when appropriate. With practice, you'll be able to leverage the power of inheritance to create robust and scalable applications.
Inspired by an article from https://css-tricks.com/on-inheriting-and-sharing-property-values/
Advertisement
Shorten Your Links, Amplify Your Reach
Tired of long, clunky URLs? Create short, powerful, and trackable links with MiniFyn. It's fast, free, and easy to use.