Thursday, September 21, 2023

Polymorphism in Typescript Part -9

 It is the characteristics of OOPS.

 The term poly means many and morphos means forms

It defines a feature that allows the component to have multiple behaviour.

Technically it is possible by allocating different types of memory for single component

Polymorphism is configuring single base class object that can use the memory of multiple derived class.


Example

class Employee
{
    public fname:string="Pawan";
    public lname:string="Kalyan";
    public print():void {
        console.log(this.fname+ "" + this.lname);
    }
}
class PartTimeEmployee extends Employee
{
    public print():void {
        this.fname="Raj";
        this.lname="Kumar";
        console.log(this.fname+ "" + this.lname);
    }
}
class FullTimeEmployee extends Employee
{
    public print():void {
        this.fname="Kiran";
        this.lname="Kumar";
        console.log(this.fname+ "" + this.lname);
    }
}
let employee=new Array();
employee[0]=new Employee();
employee[1]= new PartTimeEmployee();
employee[2]=new FullTimeEmployee();
for(var i=0;i<employee.length;i++) {
    employee[i].print();
}

Function Overloads


In TypeScript, you can create multiple functions with the same name but different parameter types and return types. But, the number of parameters should be exactly the same, only the data type can differ.

function add(a: number, b: number): number;

function add(a: string, b: string): string;


function add(a: any, b: any): any {
    let n= a + b;
    console.log(n);
}
add(5, 20);
add('Hello', 'CodingNinjas');

Output
25


Function overloading with different number of parameters and types with same name is not supported.

function display(a:string, b:string):void //Compiler Error: Duplicate function implementation
{
    console.log(a + b);
}

function display(a:number): void //Compiler Error: Duplicate function implementation
{
    console.log(a);
}

Method overriding:
------------------
derv class redeclaring base class method with the same signature is called "method overriding"

OR

derv class method name and signature is same as base class method is called
"method overriding"


Example

class person
{
    constructor(private name:string,private age:number)
    {}
    display()
    { console.log("name:"+this.name+" age:"+this.age);}
}
//derv class
class customer extends person
{
    private custid:string;
    constructor(custid:string,name:string,age:number)
    {
        super(name,age);//initializing base object
        this.custid=custid;
    }
    display()
    {
        console.log("custid:"+this.custid);
        super.display();//calling base method to display name and age
    }
}
let cobj=new customer("c001","raju",20);
cobj.display();


Output
PS C:\Examples> tsc.cmd methodoveriding.ts PS C:\Examples> node methodoveriding custid:c001 name:raju age:20


Note:
note:
-----

*Attaching access specifier to constructor paras will generate class fields
with the same name as paras and assigning paras to class fields will be
done automatically, this will reduce lot of coding to developer.

class person class person
{ {
constructor private name:string;
(private name:string, =====> private age:number;
private age:number) constructor(name:string,age:number)
{} {
this.name=name;
.. this.age=age;
} }
...
}



No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...