Answers for "what is abstract class in javascript"

2

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!
Posted by: Guest on March-05-2021
12

what is abstract class in javascript

when we use abstract classes? supposably we have class Car. 
you don't want to use it directly. you need extend this class. 
thats why you must declare this class as abstract. 
then you only can extend this class with all methods.
Posted by: Guest on October-16-2021

Code answers related to "what is abstract class in javascript"

Browse Popular Code Answers by Language