What is a class in JavaScript OOP?
A class is a blueprint for creating objects with shared structure and behavior. It defines properties and methods that instances of the class will have.
How do you define a class in JavaScript?
Using the class keyword followed by the class name and a body that includes a constructor and methods.
What is the purpose of the constructor method in a class?
The constructor initializes new object instances with given values when they are created using new.
What is an instance in OOP?
An instance is a concrete object created from a class definition. Each instance has its own copies of instance properties.
How do you create an instance of a class in JavaScript?
By using the new keyword, e.g., const dog = new Animal("Rex").
What is inheritance in OOP?
Inheritance allows one class to extend another, inheriting its properties and methods, which promotes code reuse.
How is inheritance implemented in JavaScript?
Using the extends keyword to create a subclass that inherits from a parent class.
What is the purpose of the super() function in a subclass constructor?
It calls the parent class’s constructor, ensuring the base class is properly initialized before using this.
Can JavaScript classes override parent methods?
Yes. Subclasses can define a method with the same name as one in the parent class to override it.
What does method overriding mean in OOP?
Method overriding occurs when a subclass provides a new implementation for a method already defined in its superclass.
What is encapsulation in OOP?
Encapsulation is the practice of keeping an object’s internal state private and exposing controlled access through methods or getters/setters.
How can you create private fields in modern JavaScript classes?
By prefixing the property name with a # (e.g., #balance), making it accessible only within the class body.
What are getters and setters in JavaScript?
Special methods that control access to object properties, allowing custom logic when getting or setting a value.
Why is encapsulation important?
It helps prevent unintended interference with internal data, improving maintainability and security of code.
How does JavaScript implement OOP concepts differently from traditional class-based languages?
JavaScript is prototype-based, meaning objects can directly inherit from other objects without classical inheritance.
What are prototypes in JavaScript?
Prototypes are objects that other objects inherit properties and methods from. Every JS object has an internal link to a prototype.
How does the prototype property work in constructor functions?
Methods added to a constructor’s prototype are shared by all instances, saving memory and ensuring consistent behavior.
How does class syntax relate to prototypes in JavaScript?
The class syntax is syntactic sugar over prototype-based inheritance; under the hood, it still uses prototypes.
What is polymorphism in OOP?
Polymorphism allows different objects to respond to the same method name in their own ways, often through method overriding.
What is the difference between instance and static methods?
Instance methods belong to individual objects, while static methods belong to the class itself and are called directly on the class.
Why is OOP used in JavaScript?
It helps organize code into reusable, modular pieces that model real-world entities and relationships.
What are the four main principles of OOP?
Encapsulation, Abstraction, Inheritance, and Polymorphism.
What is the difference between a class and an object?
A class is a blueprint; an object is an instance of that blueprint containing real data.
What does this keyword refer to inside a class method?
this refers to the current instance of the class on which the method was called.