CodesJava

Easy learning with example program codes

typescript class interface tutorial


TypeScript Class:

As we discussed in earlier tutorials that TypeScript supports object-oriented programming concepts like classes, interfaces, etc. Class is act as a blue print or template for creating objects. It provides state and behaviour for its objects. A class is used to encapsulate the data for the object.

TypeScript Interface:

An interface is declared with interface keyword. It is a way of implementing 100% abstraction. It only contains the declaration of the members. Interface forms a contract with your class that force your class to have all methods defined by the interface must appear in the class.
Use implements keyword to implement interfaces.

Syntax:

className implements interfaceName{}

Example:

interface IPerson { 
   name:string; 
} 
 
class Employee implements IPerson {  
   empId:string;
 
   constructor(name:string,empId:string) { 
      this.name = name; 
      this.empId = empId; 
   } 
} 
 
var obj = new Employee("Jai","EMP024");
console.log("Name: "+obj.name);
console.log("Emp Id: "+obj.empId);

Try it:

JS Bin on jsbin.com

Please follow and like us:


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki