Skip to main content

TypeScript-Inheritance

TYPESCRIPT


TypeScript - Inheritance 

Create a class "Human" which has two protected members:

  • name [type as string] and age [type as number]
  • constructor(): this initializes name and age
  • Create another class "Employee" which extends "Human" and have three members:
  • department (string), batch (string) and role (string)
  • constructor(): this initializes parent class protected members, department, batch and role.
  • getInfo(): this displays name, age, department, batch and role of the employee in a specific format.


Sample Output:
Name: person one Age: 21 Department: computer science Batch: 2019 Role: Trainee


Solution:
class Human {
  protected name: string;
  protected age: number;

  constructor(theName: string, theAge: number) {
    this.name = theName;
    this.age = theAge;
  }
}

class Employee1 extends Human {
  department: string;
  batch: string;
  role: string;

  constructor(
    name: string,
    age: number,
    theDepartment: string,
    theBatch: string,
    theRole: string
  ) {
    super(name, age);
    this.department = theDepartment;
    this.batch = theBatch;
    this.role = theRole;
  }

  getInfo() {
    console.log(
      "Name: " +
        this.name +
        "\nAge: " +
        this.age +
        "\nDepartment: " +
        this.department +
        "\nBatch: " +
        this.batch +
        "\nRole: " +
        this.role
    );
  }
}

let newEmployeeObject = new Employee1('person one',21,'computer science','2019','Trainee')
newEmployeeObject.getInfo()


Also check out -    TypeScript Class                        TypeScript Polymorphism

Comments

Popular posts from this blog

Android Architecture Stack

Hello Everyone! In this article we are discussing about the android architecture. It is essential for all the android application developers to understand the android architecture to get a clearer idea about the structure on which the application is made. From the exam point of view, the android architecture stack question has been asked several times (sometimes along with the Dalvik Virtual Machine ). Android operating system is a stack of software components which is roughly divided into five sections and four layers. The five sections of android architecture are- Linux kernel Libraries Android Runtime Application Framework Applications This is a layered architecture with each section having a different functionality. A) Linux Kernel - It is the bottom most layer of the architecture. This is the kernel on which android is based. This layer provides a level of abstraction between the hardware devices. It also contains all the l...

UML - Use Case Diagram for Vending Machine

Hello Everyone! Here is an example of an Use case Diagram of a Vending Machine System. Problem Statement - A vending machine accepts coins for variety of products. The user selects the drinks from products available through selection panel. If the drink is available the price of the product is displayed. The user then deposits the coins depending on the price of the product. Coin collector collects coin after stipulated time the controller will compare the deposited coin with price. If amount deposited is less than price then error message will be displayed and all deposited coin will be dispensed by the coin dispenser else the drink will be dispensed by the product dispenser. Check out other UML (Unified Modeling Language) Diagrams - Class Diagram   Object Diagram   Sequence Diagram Collaboration Diagram   Activity Diagram   State Diagram   Component Diagram   Deployment Diagram  

Mumbai University/BE IT/User Interaction Design/Process of Designing, Prototyping and Construction/Multiple Choice Questions - Part 3

Hello Everyone! This article is based on the common Multiple Choice Questions related to User Interface Design. The MCQs are based on the Module 4 of the syllabus- Process of Designing, Prototyping and Construction. From the University Exam point of view, these MCQs were given as question bank in the 2020 Mumbai University-BE-Information Technology Exam for the subject- User Interface Design. The MCQs set is prepared from taking different cluster data. Q 1) The use of touch in the interface is known as _______. a. Haptic Interfaces b. Multimedia Interface c. Multimodal Interface d. Gesture Interface Solution: a) Haptic Interfaces ...