Hello friends in this post we are going to discuss about Typescript JavaScript Superset – Polymorphism Fresco Play Hands-on Answer | Typescript JavaScript Objective type questions | Typescript JavaScript MCQ with answers
Type System:
A Type System is a set of rules that assign a property called type to various construct a computer program consists of, such as variables, expressions, functions, or modules.
Type checker is responsible for Type Checking.
It is a kind of program analyser verifying the types that are being used in the code. The soul purpose is to recognize the bugs before the execution of the code.
Two types of Type Checking:
- Static Type Checking
- Dynamic Type Checking
TypeScript:
Inheritance:
JavaScript uses prototypical inheritance instead of classical inheritance
Hands-on Human Inheritance Answer:
TypeScript- Human Inheritance:
Create class” Human” which has two protected members
- name [type a string] and age [type as number]
- Constructor (): this initializers name and age.
- Create another class “Employee” which extends “Human” and have three members:
- department [type a string], batch [type string] and role [type as string]
- Constructor (): this initialises parent class protected members, department, batch, and role
- get Info(): 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
Roll: trainee
Hands-on Answer:
class Human
{
Name:string;
Age:number;
constructor (protected name: string, protected age: number)
{
this.Name = name;
this.Age = age;
}
}
class Employee extends Human
{
Department: string;
Batch: string;
Role: string;
constructor(name: string, age: number, protected department: string, protected batch: string, protected role: string)
{
Super(name, age) ;
this.Department = department;
this.Batch = batch;
this.Role = role;
}
getInfo()
{
console.log(`Name: ${this.name}\nAge: ${this.age}\nDepartment: ${this.department}\nBatch:${this.batch}\nRole:${this.role}`;
}
}
=======================================
Polymorphism – Overriding
Method overriding is a mechanism by which the child class redefines the superclass method.
Hands-on Automobile Polymorphism Answer:
TypeScript- Automobile Polymorphism:
Create a class “Automobile” that has two protected properties and a method:
- fuelType and price
- Initialise the properties using constructor
- Print info (): displays “I am automobile” in console
- Now create another class “Car” which extends the above class and has two functions
- Constructor: this initialise “fuelType” ans “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
Hands-on Answer:
class Automobile
{
FuelType:any;
Price:any;
constructor(protected fuelType: any, protected price: any)
{
this.FuelType = fuelType;
this.Price = price;
}
printInfo()
{
console.log(`I am automobile`) ;
}
}
class Car extends Automobile
{
constructor (fuelType: any, price: any ) {
Super(fuelType, price) ;
}
printInfo()
{
Super.printInfo() ;
console.log(`Fuel Type: ${this. fuelType }\nPrice: ${this. price }`) ;
}
}