TypeScript – Classes and Interfaces

In this post, we will see how to use Classes and Interfaces in TypeScript.

CLASSES

// definition of a class called User

class User{
    
    // private variables
    private _id: number;
    private _username: string;
    private _password: string;

    // constructor
    constructor(id: number, username: string, password: string)
    {
        this._id = id;
        this._username = username;
        this._password = password;
    }

    // method void
    PrintHello() {
        console.log(`Hello ${this._username}`);
    }

    // method with parameter in output
    GetHello():string {
        return `Hello ${this._username}`;
    }

    PrintHello2()
    {
        this.GetHelloPrivate();
    }

    // private method
    private GetHelloPrivate()
    {
        console.log('Print from GetHello2');
    }
}

// Creating an Object of User
let objUser = new User(1,'Username1', 'Password1');
objUser.PrintHello();
console.log(objUser.GetHello());
objUser.PrintHello2();



[Inheritance]

// definition of a class called User
class User{
    
    // private variables
    private _id: number;
    private _username: string;
    private _password: string;

    // constructor
    constructor(id: number, username: string, password: string)
    {
        this._id = id;
        this._username = username;
        this._password = password;
    }

    // method void
    PrintInfo() {
        console.log(`${this._id} - ${this._username}`);
    }
}



// definition of a class called Admin that inherits from User
class Admin extends User{

    key: string;

    constructor(id: number, username: string, password: string, key: string)
    {
        super(id, username, password);
        this.key = key;
    }

    PrintInfoByUser(){
        this.PrintInfo();
    }

    PrintKey(){
        console.log(`Your key is ${this.key}`);
    }
}


let objAdmin = new Admin(1, 'Username', 'Password', 'Key12345');

objAdmin.PrintInfoByUser();
objAdmin.PrintKey();



[abstract class]

abstract class User {
    _userName: string;
    _password: string;

    constructor(userName: string, password:string) {
        this._userName = userName;
        this._password = password;
    }

    // definition of a method
    ShowPassword(){
        console.log(`The password is: ${this._password}`);
    }

    // definition of an abstract method
    abstract WhoAmI():string;
}


class Admin extends User
{
    private _id: number;

    constructor(userName: string, password:string)
    {
        // constructor of User
        super(userName,password);
        // definition of a number between 0 and 999
        this._id = Math.floor(Math.random() * 1000);
    }

    // definition of a method called PrintId
    PrintId(){
        console.log(`Your Id is: ${this._id}`);
    }

    //definition of WhoAmI
    WhoAmI():string
    {
        return `${this._id} - ${this._userName} - ${this._password}`
    }
}


let objAdmin = new Admin('UserName', 'Password');
objAdmin.PrintId();
console.log(objAdmin.WhoAmI());
objAdmin.ShowPassword();


INTERFACES

interface User{
    _userName: string;
    _password: string;

    ShowPassword():void;

    WhoAmI():string;
}


class Admin implements User
{
    private _id: number;
    _userName: string;
    _password: string;

    constructor(userName: string, password:string)
    {
        this._userName = userName;
        this._password = password;
        // definition of a number between 0 and 999
        this._id = Math.floor(Math.random() * 1000);
    }
    

    //definition of ShowPassword
    ShowPassword(): void {
        console.log(`Your password is: ${this._password}`);
    }

    //definition of WhoAmI
    WhoAmI():string
    {
        return `${this._id} - ${this._userName} - ${this._password}`
    }
}


let objAdmin = new Admin('UserName', 'Password');
console.log(objAdmin.WhoAmI());
objAdmin.ShowPassword();