showing results for - "javascript abstract class"
Randall
07 May 2017
1//there is no way to mark a class as abstract in ES6,
2//however you can force a class to behave line one by
3//	- forcing derived classes to override a method
4//	- causing the base class's contructor to throw an error so that it
5//			is never used to create instances of the base type*
6// *Be careful, as this will cause problems if you do need derived classes
7// 	to call super() contructor
8class Foo {
9 
10    constructor(text){
11       this._text = text;
12    }
13 
14    /**
15     * Implementation optional
16     */
17    genericMethod() {
18        console.log('running from super class. Text: '+this._text);
19    }
20    
21    /**
22     * Implementation required
23     */
24    doSomething() {
25       throw new Error('You have to implement the method doSomething!');
26    }
27 
28}
29 
30class Bar extends Foo {
31 
32    constructor(text){
33       super(text);
34    }
35 
36    genericMethod() {
37        console.log('running from extended class. Text: '+this._text);
38    }
39    
40    doSomething() {
41       console.log('Method implemented successfully!');
42    }
43    
44}
45 
46let b = new Bar('Howdy!');
47b.genericMethod(); //gonna print: running from extended class. Text: Howdy
48b.doSomething(); //gonna print: Method implemented successfully!
similar questions
queries leading to this page
javascript abstract class