OOPs вопросы и ответы для интервью
Вопрос 6. What is a constructor?
A constructor is a special method in a class that is automatically called when an object of the class is created. It is used for initializing object properties.
Example:
class Car {
constructor(make, model) {
this.make = make;
this.model = model;
}
}
Вопрос 7. Explain the concept of method overloading.
Method overloading is a feature in OOP where a class can have multiple methods with the same name but with different parameter types or a different number of parameters.
Example:
class MathOperations {
add(a, b) {
return a + b;
}
add(a, b, c) {
return a + b + c;
}
}
Вопрос 8. What is the 'super' keyword used for?
The 'super' keyword is used to call the constructor or methods of the parent class in a subclass. It is often used to access the superclass's methods or properties.
Example:
class Child extends Parent {
constructor() {
super();
}
}
Вопрос 9. What is an interface in OOP?
An interface in OOP defines a contract for classes, specifying a set of methods that a class must implement. It allows for achieving multiple inheritance in languages that don't support it directly.
Example:
interface Printable {
print();
}
Вопрос 10. Explain the concept of abstract classes.
Abstract classes are classes that cannot be instantiated and may contain abstract methods. Subclasses must implement these abstract methods.
Example:
abstract class Shape {
abstract calculateArea();
}
Самое полезное по оценкам пользователей: