js abstract class code example

Example: javascript abstract class

//there is no way to mark a class as abstract in ES6,
//however you can force a class to behave line one by
//	- forcing derived classes to override a method
//	- causing the base class's contructor to throw an error so that it
//			is never used to create instances of the base type*
// *Be careful, as this will cause problems if you do need derived classes
// 	to call super() contructor
class Foo {
 
    constructor(text){
       this._text = text;
    }
 
    /**
     * Implementation optional
     */
    genericMethod() {
        console.log('running from super class. Text: '+this._text);
    }
    
    /**
     * Implementation required
     */
    doSomething() {
       throw new Error('You have to implement the method doSomething!');
    }
 
}
 
class Bar extends Foo {
 
    constructor(text){
       super(text);
    }
 
    genericMethod() {
        console.log('running from extended class. Text: '+this._text);
    }
    
    doSomething() {
       console.log('Method implemented successfully!');
    }
    
}
 
let b = new Bar('Howdy!');
b.genericMethod(); //gonna print: running from extended class. Text: Howdy
b.doSomething(); //gonna print: Method implemented successfully!