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 Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

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