JavaScript has classes, but underneath they are objects linked to other objects through prototypes — and interviewers love to check whether you know that. This guide covers prototypal inheritance, ES6 classes, Object.create versus new, encapsulation, getters, setters and statics, mixins, property lookups and freezing objects.
Why interviewers ask about OOP and prototypes
Developers who arrive from Java or C# often treat JavaScript classes as identical to the ones they know, and then get surprised by this, by methods shared through the prototype, or by inheritance that can be changed at runtime. These questions check that you understand the model the language actually uses.
The topic also comes up in framework work: React class components, Angular services and Node.js libraries all rely on classes and prototypes behaving the way the specification says.
How to answer these questions
Explain each idea twice: once with class syntax and once with the prototype it creates. Saying "a class method lives on ClassName.prototype and every instance looks it up through the chain" proves you understand what the syntax hides.
Many of these answers depend on how this is set, so revisit the closures and functions guide if that rule is not automatic yet. Coming from Java? The Java vs JavaScript comparison explains the differences in the object model.
Q1. What is prototypal inheritance in JavaScript?
Objects inherit properties and methods from other objects through the prototype chain. Every object has an internal [[Prototype]] link, and property lookups walk up that chain until they find a match or reach null.
function Animal(name) { this.name = name; }
Animal.prototype.speak = function () {
return `${this.name} makes a sound.`;
};
const dog = new Animal("Rex");
dog.speak(); // "Rex makes a sound."
// Object.getPrototypeOf(dog) === Animal.prototype → true
Q2. What are ES6 classes?
ES6 classes are cleaner syntax over prototypal inheritance — class, constructor, extends and super — but underneath, methods still live on the prototype.
class Animal {
constructor(name) { this.name = name; }
speak() { return `${this.name} makes a sound.`; }
}
class Dog extends Animal {
speak() { return `${this.name} barks!`; }
}
const d = new Dog("Rex");
d.speak(); // "Rex barks!"
d instanceof Animal; // true
Interview note: Follow-up: "what is
typeofa class?" It is"function"— a class is a special constructor function, which shows it is built on prototypes.
Q3. What is the difference between Object.create() and the new keyword?
new Fn() creates an object whose prototype is Fn.prototype and runs the constructor. Object.create(proto) creates a plain object with proto as its prototype and runs no constructor.
const proto = { greet() { return `Hi, ${this.name}`; } };
const obj = Object.create(proto);
obj.name = "Siva";
obj.greet(); // "Hi, Siva"
// new keyword
function Person(name) { this.name = name; }
const p = new Person("Siva");
p.greet; // undefined — there is no greet on Person.prototype
Q4. What is encapsulation and how is it achieved in JavaScript?
Encapsulation bundles data with the methods that use it and hides internal state. In JavaScript you get it with closures (private variables in a factory function) or class private fields (#field), which the engine enforces.
class BankAccount {
#balance = 0;
deposit(amount) { this.#balance += amount; }
get balance() { return this.#balance; }
}
const acc = new BankAccount();
acc.deposit(500);
acc.balance; // 500
acc.#balance; // SyntaxError — truly private
Q5. What are getters, setters and static members in a class?
Getters and setters (get/set) run code when a property is read or assigned, so you can compute or validate values. static members belong to the class itself, not to its instances — you call them as ClassName.method().
class Temperature {
static unit = "°C";
#celsius = 0;
get fahrenheit() { return this.#celsius * 9 / 5 + 32; }
set celsius(v) {
if (typeof v !== "number") throw new TypeError("Number expected");
this.#celsius = v;
}
static fromFahrenheit(f) {
const t = new Temperature();
t.celsius = (f - 32) * 5 / 9;
return t;
}
}
const t = Temperature.fromFahrenheit(212);
t.fahrenheit; // 212
Temperature.unit; // "°C" — on the class, not on t
Interview note: Follow-up: "can an instance call a static method?" Not directly —
t.fromFahrenheitisundefined, because static methods live on the constructor.
Q6. What are mixins in JavaScript?
Mixins add reusable behaviour to a class without a deep inheritance chain — typically as functions that take a base class and return an extended one.
const Serializable = (Base) => class extends Base {
serialize() { return JSON.stringify(this); }
};
const Validatable = (Base) => class extends Base {
isValid() { return Object.keys(this).length > 0; }
};
class User extends Serializable(Validatable(class {})) {
constructor(name) { super(); this.name = name; }
}
new User("Siva").serialize(); // '{"name":"Siva"}'
new User("Siva").isValid(); // true
They help avoid the fragile base-class problem of classical inheritance.
Q7. What is the difference between the in operator and hasOwnProperty?
in checks the object and its whole prototype chain. hasOwnProperty — or the newer Object.hasOwn — checks only the object's own properties.
const base = { greet() {} };
const obj = Object.create(base);
obj.name = "Ravi";
"name" in obj; // true
"greet" in obj; // true — found on the prototype
Object.hasOwn(obj, "greet"); // false — not an own property
Object.hasOwn(obj, "name"); // true
A for...in loop also walks inherited enumerable properties, which is why older code filtered it with hasOwnProperty. Object.keys() returns own properties only.
Q8. What is the difference between Object.freeze() and const?
const stops the variable from being reassigned; Object.freeze() stops the object itself from changing — no adding, removing or editing properties. Freezing is shallow, so nested objects can still change.
const config = Object.freeze({ env: "prod", db: { host: "a" } });
config.env = "dev"; // ignored (TypeError in strict mode)
config.db.host = "b"; // works — freeze is shallow
config.db.host; // "b"
Object.seal() sits in between: existing properties can be edited, but none can be added or removed.
Interview note: Follow-up: "how do you deep-freeze an object?" Call
Object.freezerecursively on every nested object.
How to prepare
Model one small domain — a bank account or a shopping cart — three ways: a factory function with closures, a constructor function with a prototype, and an ES6 class with private fields. Explain which you would choose and why.
Then continue to advanced JavaScript, where Symbols, Proxies and iterators extend the same object model.
Frequently Asked Questions
Is JavaScript an object-oriented language?
What is the prototype chain?
What is the difference between __proto__ and prototype?
How do you make a property private in JavaScript?
Does JavaScript support multiple inheritance?
Should I use classes or factory functions?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Check the Java Full Stack training details

