How to use class method overriding in TypeScript

1 Answer

0 votes
class Car {
    cType: string;
        
    constructor(ctype: string) {
        this.cType = ctype;
    }
    
    run(cost:number = 0) {
        console.log(this.cType + " " + cost);
    }
}

class Lexus extends Car {
    
    constructor(ctype: string) {
        super(ctype);
    }
    
    run(cost = 91830) {
        console.log('class Lexus extends Car')
        super.run(cost);
    }
}

class Ram extends Car {
    
    constructor(ctype: string) {
        super(ctype);
    }
    
    run(cost = 58645) {
        console.log('class Ram extends Car')
        super.run(cost);
    }
}

let mObj = new Lexus("Lexus LX 2021");
let rObj = new Ram("Ram 1500 2021")

mObj.run(); 
rObj.run(); 


    
 

    
/*
    
run:
    
"class Lexus extends Car" 
"Lexus LX 2021 91830" 
"class Ram extends Car" 
"Ram 1500 2021 58645" 
   
*/

 



answered Oct 25, 2021 by avibootz

Related questions

1 answer 274 views
274 views asked Jan 4, 2022 by avibootz
3 answers 517 views
1 answer 237 views
1 answer 288 views
1 answer 225 views
1 answer 213 views
...