Answer the question
In order to leave comments, you need to log in
How to correctly override a method in a child class?
I have a stupid situation, but I will give an example-synonymous with my code
class Parent {
constructor(){
}
methodA(){
// ..
this.methodB();
// ..
}
methodB(){
// empty because need override in child class
}
};
class Child1 extends Parent {
constructor(){
super();
}
methodB(){
// my actions
}
}
Answer the question
In order to leave comments, you need to log in
The first option is to make the parent class abstract and declare an abstract method methodB in it. The compiler will require that methodB must be declared in the descendant class
abstract class Parent {
methodA(){
this.methodB();
}
abstract methodB(): void
};
class Child1 extends Parent { // Ошибка: Non-abstract class 'Child1' does not implement inherited abstract member 'methodB' from class 'Parent'.
constructor(){
super();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question