Understanding Class-Based and Prototype-Based Inheritance

Ritvik Saran SRitvik Saran S
3 min read

When diving deep into object-oriented programming, two primary paradigms stand out: class-based inheritance (as seen in Java, C#, etc.) and prototype-based inheritance (as seen in JavaScript). Though they work differently under the hood, both aim to achieve the same goal:- code reuse and modularity.

Let’s break down how objects, classes, and inheritance work in both paradigms and how memory management plays a key role in optimizing performance.

Class-Based Inheritance (Java, C#)

In languages like Java, the class serves as a blueprint for creating objects. A class defines the structure and behavior (methods) that objects of that class will have.

How Classes and Objects Are Stored in Memory

  • When a class is loaded, its methods and static members are stored in a common Method Area in memory. This is shared across all instances (objects) of the class.

  • When an object is instantiated, it gets its own space in the Heap Memory, but it does not create copies of methods. Instead, all objects refer back to the method definitions stored in the Method Area.

  • Instance variables (or fields) are unique to each object, meaning every object has its own copy of data, but the methods remain shared.

Example in Java

Here’s what happens in memory:

  1. The Car class is loaded, and its methods (drive) are stored in the Method Area.

  2. When car1 and car2 are created, they get separate memory allocations in the Heap, storing their model values but referring to the same method in the Method Area.

This efficient memory management avoids duplicating methods for each object, keeping memory usage optimized.

Prototype-Based Inheritance (JavaScript)

Unlike Java, JavaScript does not have classical classes in its original form. Instead, it uses prototypes, which serve as shared blueprints for objects.

How JavaScript Objects and Prototypes Work in Memory

  • JavaScript objects inherit directly from other objects using the prototype chain.

  • A constructor function initializes an object, and methods are attached to its prototype.

  • Instead of storing methods separately for each instance, all objects of a "class" (function) share the same prototype.

Example in JavaScript

How This Works in Memory

  1. The Car constructor function creates new objects, assigning unique model values in Heap memory.

  2. The drive function is stored in the Car.prototype object, shared by all instances.

  3. When car1.drive() is called, JavaScript looks for the drive method in car1. Since it’s not directly inside car1, it follows the prototype chain to find it in Car.prototype.

This behavior ensures that methods are not duplicated for every object, making the system memory-efficient, just like Java’s method storage in the Method Area.

In upcoming posts, I’ll dive deeper into both paradigms, explaining their internal workings, best practices, and real-world applications. Stay tuned!

0
Subscribe to my newsletter

Read articles from Ritvik Saran S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ritvik Saran S
Ritvik Saran S

Self Taught Software Engineer at TeamLease Services Limited🌟 | Nitian 🎓 | Expanding My Tech Horizon and Sharing the Excitement 🔥