OOPs Interview Questions and Answers
Ques 11. What is the 'this' keyword in OOP?
The 'this' keyword refers to the current instance of the class. It is used to access the current object's properties and methods within that class.
Example:
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log('Hello, ' + this.name + '!');
}
}
Ques 12. Explain the concept of method overriding.
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. It allows a subclass to provide a specialized version of a method.
Example:
class Animal {
speak() {
console.log('Animal speaks');
}
}
class Dog extends Animal {
speak() {
console.log('Dog barks');
}
}
Ques 13. What is a static method in a class?
A static method is a method that belongs to the class rather than an instance of the class. It is called on the class itself, not on an object created from the class.
Example:
class MathUtils {
static add(a, b) {
return a + b;
}
}
Ques 14. What is the difference between composition and inheritance?
Composition involves combining objects to create more complex ones, while inheritance involves creating a new class by inheriting properties and behaviors from an existing class.
Ques 15. What is a destructor?
A destructor is a method that is called when an object is about to be destroyed. In many programming languages, like C++, it is used to release resources held by the object.
Example:
class MyClass {
destructor() {
// clean up resources
}
}
Most helpful rated by users: