Thursday, September 21, 2023

Inheritance In Typescript Part 8

 Inheritance:

It is one of the key feature of OOPS.

It enables code extensibility and allows loosely coupled architecture.

It is the process of creating a new class that extends existing class.

The newly created class is known as derived class and existing class is known as super class.

typescript is providing "extends" to work with inheritance

Syntax:


class Super

{

    //Super Class Members

}

class Derived extends Super {

//Derived class Members

}

The code can be reused by using Inheritance which is known as Has a Relation

The members of super class are accessible in derived class by using the keyword "this"

Syntax with constructor

syntax:
   class <derv|sub classname>  extends <base|super classname>
   {
     constructor(--)
     {
        super(--); //calling base constructor to initialize base class fields
        initialize derv class fields
     }
     <methodname>(--)
     {
       super.<methodname>(--);
        ...
     }
      ...
   }
class Base
{
    public x:number=10;
}
class Derived extends Base{
    public y:number=20;
    public print():void {
        console.log("x=" + this.x + "\n" + "y=" + this.y);
    }
}
let objderived=new Derived();
objderived.print();

Output:

x=10 y=20


Will typescript supports multiple inheritance for a class
No

Why multiple inheritance is not supported for classes.
Inheritance depends on constructor, when you create a derived class object, first
base class constructor is called than followed by derived. If they are multiple base
classes then there will be conflicts between the constructors. Hence multiple inheritance
is not supported.

MultiLevel Inheritance

It is the process, where in derived class can be extended by new class , so that
it can be access both derived and super class members.

Example

class ProductId {
    public Id:number;
}
class Details extends ProductId
{
    public Name:string;
    public Price:number;
}
class ProductInfo extends Details
{
    public print():void {
        console.log("Id="+ this.Id);
        console.log("Name="+ this.Name);
        console.log("Price="+ this.Price);
    }
}
let objproductInfo=new ProductInfo();
objproductInfo.Id=1;
objproductInfo.Name="Samsung";
objproductInfo.Price=4353;
objproductInfo.print();


Output:

Id=1
Name=Samsung
Price=4353



Example 2

class Department {
    name: string;
   
    constructor(name: string) {
        this.name = name;
    }
}

class  Employee extends Department {
    empCode: number;
   
    constructor(empcode: number, name:string) {
        super(name);
        this.empCode = empcode;
    }
   
    displayName():void {
        console.log("Department Name = " + this.name +  ", Employee Code = " + this.empCode);
    }
}

let emp = new Employee(100, "IT");
emp.displayName();


Output:

Department Name = IT, Employee Code = 100

Note: In the above example
The constructor of the Employee class initializes its own members as well as the parent class's properties using a special keyword 'super'. The super keyword is used to call the parent constructor and passes the property values.


Class implements Interface


interface IPerson {
    name: string;
    display():void;
}

interface IEmployee {
    empId: number;
}

class Employee implements IPerson, IEmployee {
    empId: number;
    name: string;
   
    constructor(empId: number, name:string) {
        this.empId = empId;
        this.name = name;
    }
   
    display(): void {
        console.log("Name = " + this.name +  ", Employee Id = " + this.empId);
    }
}

let per:IPerson = new Employee(100, "Uday");
per.display(); // Name = Bill, Employee Code = 100

let emp:IEmployee = new Employee(100, "Uday");
emp.display(); //Compiler Error: Property 'display' does not exist on type 'IEmployee'



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...