While ES6 does not have abstract methods or properties, is it possible to access some methods or properties from the parent class in an inherited class?
class ParentClass {
constructor(){
ParentClass.checkChildPropertyAccessibility();
ParentClass.checkChildMethodAccessibility();
ParentClass.checkChildStaticPropertyAccessibility();
ParentClass.checkChildStaticMethodAccessibility();
}
static checkChildPropertyAccessibility() {
console.log(ParentClass.childProperty);
}
static checkChildMethodAccessibility(){
ParentClass.childMethod()
}
static checkChildStaticPropertyAccessibility(){
console.log(ParentClass.childStaticProperty);
}
static checkChildStaticMethodAccessibility(){
ParentClass.childStaticMethod()
}
}
class ChildClass extends ParentClass {
constructor(){
super();
ChildClass.childProperty = 'child\'s Property: OK';
}
childMethod(){
console.log('child\'s method OK');
}
// static property emulation in ES6
static get childStaticProperty() { return 'Child\'s static property: OK even in ES6' }
static childStaticMethod (){
console.log('Child\'s static method: OK');
}
}
let childClassInstance = new ChildClass();
The idea here is that "we need to define certain properties and methods in the child class, however, the parent class should already be able to access them in the constructor".