Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,817 answers

573 users

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 137 views
137 views asked Jan 4, 2022 by avibootz
3 answers 339 views
1 answer 122 views
1 answer 181 views
1 answer 109 views
1 answer 106 views
...