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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book