...

Web Technologies - TypeScript

Back to Course

Lesson Description


Lession - #1452 TypeScript Classes


The class in TypeScript is compiled to plain JavaScript functions by the TypeScript compiler to work across platforms and browsers.

A class can include the following
  • Constructor
  • Properties
  • Methods
  • The following is an illustration of a class in TypeScript

    
     Example Class Copy 
     class Employee{ 
     empCode number; 
     empName string; 
     
     constructor( code number, name string>
    { this.empName = name; = code; getSalary(>
    number{ return 10000;


    The TypeScript compiler will convert the above class to the following JavaScript code using closure

    
     var Employee = / **@class */( function(>
    { function Employee( name, code>
    { this.empName = name; = code; = function(>
    { return 10000; ; return Employee; (>
    >
    ;


    Constructor
    The constructor is a special type of method which is called when creating an object. In TypeScript, the constructor method is always defined with the name" constructor".

    Example Constructor
    
     
     class Employee{ 
     
     empCode number; 
     empName string; 
     
     constructor( empcode number, name string>
    { this.empCode = empcode; = name;


    In the above illustration, the Employee class includes a constructor with the parameters empcode and name. In the constructor, members of the class can be accessed using this keyworde.g.this.empCode orthis.name.

    It isn't necessary for a class to have a constructor.

    
     Example Class without Constructor 
     class Employee{ 
     empCode number; 
     empName string; 
     


    Creating an Object of Class
    An object of the class can be created using the new keyword.

    illustration Create an Object
    
     class Employee{ 
     empCode number; 
     empName string; 
     
     
     let emp = new Employee(>
    ;


    Inheritance
    Just like object- oriented languages similar as Java andC#, TypeScript classes can be extended to create new classes with inheritance, using the keyword extends.

    illustration Inheritance

    
     class Person{ 
     name string; 
     
     constructor( name string>
    { = name; class Employee extends Person{ empCode number; constructor( empcode number, namestring>
    { super( name>
    ; = empcode; displayName(>
    void{ (" Name = "this.name", Employee Code = "this.empCode>
    ; let emp = new Employee( 100," Bill">
    ; emp.displayName(>
    ;// Name = Bill, Employee Code = 100


    Interface extends Class
    An interface can also extend a class to represent a type.

    
     Example Interface Extends Class 
     class Person{ 
     name string; 
     
     
     interface IEmployee extends Person{ 
     empCode number; 
     
     
     let emp IEmployee = { empCode 1, name" James Bond"} 
     


    In the above illustration, IEmployee is an interface that extends the Person class. So, we can declare a variable of type IEmployee with two properties. So now, we must declare and initialize values at the same time.

    Method Overriding
    When a child class defines its own implementation of a method from the parent class, it's called method overriding.

    Example Method Overriding

    
     class Car{ 
     name string; 
     
     constructor( name string>
    { = name; run( speednumber = 0>
    { (" A"this.name" is moving at" speed" mph!">
    ; class Mercedes extends Car{ constructor( name string>
    { super( name>
    ; run( speed = 150>
    { (' A Mercedes started'>
    ( speed>
    ; class Honda extends Car{ constructor( name string>
    { super( name>
    ; run( speed = 100>
    { (' A Honda started'>
    ( speed>
    ; let mercObj = new Mercedes(" Mercedes- Benz GLA">
    ; let hondaObj = new Honda(" Honda City">
    (>
    ;// A Mercedes started A Mercedes- Benz GLA is moving at 150 mph! (>
    ;// A Honda started A Honda City is moving at 100 mph!


    Online compiler