Thursday, 9 October 2025

Typescript Inheritance example

 //Type following code in Typescript online compiler to test it


class NamedSpace {

  

  constructor(public rank: number, private status: boolean){}

  doGet(name:string): string{

    let frank : string = null;

     if(this.status){

     frank = `${name} scored ${this.rank} in class`;

     

     }

     else

      frank = `${name} didnot score good rank so is demoted`;

      

      return frank;

  

}


doValue(address:string, name: string){

  

  let zoned: string;

  if(this.rank <15){

    zoned = `${name} is living in zone 17 correspoding to ${address}`;

  }

  else if(this.rank > 25){

    

    zoned = `${name} is living in zone 36 correspoding to ${address}`;

  }

  else 

    zoned = `${name} is living in zone 97 corresponding to ${address}`

    



   return zoned;

}

}


class UberSpace extends NamedSpace{

  

  constructor(rank: number, status: boolean, private deviation: number, private city: string){

    super(rank, status);

  }

  

  

    doPut(Uber: string, timeZone: string): string{

    

    let Uberdriver: string;

    

    console.log(this.deviation);

    console.log(Uber + " " + timeZone);

    switch(this.deviation){

    

    case 10:

      Uberdriver = ` the driver with ${this.rank} should drive ${this.city} in ${timeZone}`;

      break;

    case 20:

       this.status = true;

       console.log(super.doGet("Johhny"));

        Uberdriver = `the driver will be driving ${Uber} and ready to drive in ${this.city} in ${timeZone}`;

        break;

    case 30:

      Uberdriver = `the driver should drive in town in any timezone`;

      break;

    default:

        this.status = true;

        console.log(super.doGet("Ash"));

        Uberdriver = `the driver will be driving ${Uber} and ready to drive ${this.city} in ${timeZone}`;

        break;}

    return Uberdriver;

  }

  

  

  

}





const JustSpace = new NamedSpace(23, true);

console.log(JustSpace.doGet('john'));

console.log(JustSpace.doValue('Sandord Street, Hmailton','Rahuse'));

const UberTest = new UberSpace(23, true, 1, 'Texas');

console.log(UberTest.doPut('Toyota', 'EST'));



///Output:

Hello, World!
john scored 23 in class
Rahuse is living in zone 97 corresponding to Sandord Street, Hmailton
1
Toyota EST
Ash scored 23 in class
the driver will be driving Toyota and ready to drive Texas in EST

No comments:

Post a Comment