OOPs Interview Questions and Answers
Ques 21. Explain the term 'constructor chaining'.
Constructor chaining refers to the process of calling one constructor from another in the same class or a superclass. It allows for reusing code and initializing objects more efficiently.
Example:
class Vehicle {
constructor(make) {
this.make = make;
}
}
class Car extends Vehicle {
constructor(make, model) {
super(make);
this.model = model;
}
}
Ques 22. What is the diamond problem in the context of multiple inheritance?
The diamond problem occurs when a class inherits from two classes that have a common ancestor. It can lead to ambiguity in the inheritance hierarchy, especially if the common ancestor has members.
Ques 23. What is an interface in Java?
In Java, an interface is a collection of abstract methods. A class implements an interface, providing concrete implementations for all its methods. It allows for achieving multiple inheritance in Java.
Example:
interface Printable {
void print();
}
Ques 24. What is the purpose of the 'final' keyword?
In OOP, the 'final' keyword can be applied to classes, methods, or variables. It indicates that a class cannot be extended, a method cannot be overridden, or a variable cannot be reassigned.
Example:
final class MyFinalClass {
// class content
}
Ques 25. Explain the concept of method hiding.
Method hiding occurs when a subclass provides a static method with the same signature as a static method in its superclass. It does not override the method but hides it.
Example:
class Parent {
static show() {
console.log('Static method in Parent');
}
}
class Child extends Parent {
static show() {
console.log('Static method in Child');
}
}
Most helpful rated by users: