Skip to main content

Posts

Showing posts with the label OOPs

TypeScript - Polymorphism

TYPESCRIPT  TypeScript - Polymorphism Create a class " Automobile " that has two protected properties and a method: fuelType and price initialize the properties using constructor printInfo(): displays "I am automobile" in console Now create another class "Car" which extends the above class and has two functions constructor: this initializes "fuelType" and "price" by invoking the base class constructor printInfo(): displays Fuel type and price of that object in a format specified below // overriding function Fuel Type: <fuelType> Price: <price> Sample Output: I am automobile Fuel Type: Petrol Price: 100 Note: Inputs are passed in the constructor of the class Car Solution: class Automobile {   protected fuelType : string ;   protected price : number ;   constructor ( theFuelType : string , thePrice : number ) {     this . fuelType = theFuelType ;     this . price = thePrice ;   }   printInfo () {     con...

TypeScript-Inheritance

TYPESCRIPT TypeScript - Inheritance  Create a class " Human " which has two protected members: name [type as string] and age [type as number] constructor(): this initializes name and age Create another class " Employee " which extends " Human " and have three members: department (string), batch (string) and role (string) constructor(): this initializes parent class protected members, department, batch and role. getInfo (): this displays name, age, department, batch and role of the employee in a specific format. Sample Output: Name: person one Age: 21 Department: computer science Batch: 2019 Role: Trainee Solution: class Human {   protected name : string ;   protected age : number ;   constructor ( theName : string , theAge : number ) {     this . name = theName ;     this . age = theAge ;   } } class Employee1 extends Human {   department : string ;   batch : string ;   role : string ;   constructor (  ...

TypeScript-Class

T YPESCRIPT TypeScript - Class Create a class " Restaurant " that has one public member "menu" to store today's menu list a constructor to initialize it, and function "list()" that display today's menu list In list() function, log the menu. Sample Output: ['dosa','idli','chaat'] Note: Input array is passed in the constructor of the class Restaurant Solution: class Restaurant {     public menu : string [];       constructor ( theMenu : string []) {       this . menu = theMenu ;     }       list () {       console . log ( this . menu );     }   } let newRestaurantObject = new Restaurant ([ 'dosa','idli','chaat' ]) newRestaurantObject . list () Also c heck out - TypeScript Inheritance TypeScript Polymorphism