TypeScript Inheritance and Examples

TypeScript - Inheritance and Examples

Inheritance - TypeScript is supports the concept of Inheritance.
Inheritance has ability of a program to extend existing classes to create new ones.

The extended class is called parent class or super class and the newly created classes are called child class or sub class.

Inheritance can be classified as -
1.     Single - every class can at the most extend from one parent class
2.     Multiple - doesn’t support multiple inheritances in TypeScript.
3.     Multi-level

Stayed Informed – Learn Angular 2 with TypeScript

A class captains the Public, Private, Protected and Read-only modifiers and Public by default.
You can see the below example, the class User and each members are public by default.

Example as,
class Employee {
    empName: string;
    constructor(name: string) {
        this.empName = name;
    }

    salary(salary: number = 10000) {
        console.log('Hello, ' + this.empName + ' Your Salary -' + salary);
    }
}

class Employer extends Employee {
    constructor(empName: string) {
        super(empName);
    }

    salary(salary = 20000) {
        super.salary(salary);
    }
}

let empSal = new Employee("Anil");
console.log(empSal.salary());
console.log(empSal.salary(40000));

I hope you are enjoying with this post! Please share with you friends. Thank you!!

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^