Difference between interfaces and classes in TypeScript
Last Updated :
13 Dec, 2022
In this article, we will see what is the Difference between “interface” and “Classes” in TypeScript.
Interface: Interface is the virtual structure that is used for type-checking. In TypeScript we use interface keyword to create the new interface with identity. It create the structure for the same datatype. Interface is the structure which define the properties and method for object with name and type.
Syntax:
interface New_Interface{ // This is interface Body }
Features:
- It has loose coupling.
- It supports multiple Inheritance.
Example 1:
Javascript
interface ForClass {
readonly var1:string;
}
let newclass: ForList = {var1: "Interface" };
console.log(newclass);
|
Output:
{ var1: 'Interface' }
Example 2:
Javascript
interface ForObj {
First: string
}
interface forNewObj extends ForObj {
Second: number
}
let newArray: forNewObj = {
First: "Interface for Object" ,
Second: 2
};
console.log(newArray);
|
Output:
{ First: 'Interface for Object', Second: 2 }
Classes: They are the skeleton of objects with its use we implement objects. In TypeScript, we use class Keyword to create the constructor for the object. It can have properties, methods and variables.
Syntax:
class geeks {
// Class property and methods
// are created here
}
Features:
- In classes it support member visibility.
- It supports member overriding.
- it supports inheritance.
Example 1:
Javascript
const Table_Object = {
Size : 32,
contents : [ "Book" , "Pen" ],
print : function () {
console.log( "hello Geeks" )
}
}
console.log(Table_Object);
|
Output:
{
Size: 32,
contents: [ 'Book', 'Pen' ],
print: [Function: print]
}
Example 2:
Javascript
class Geeks {
name : string ;
articles: number ;
cp_problems: number;
constructor(name:string, articles: number, cp_problems: number) {
this .name = name;
this .articles = articles;
this .cp_problems = cp_problems;
}
About() : void {
console.log( "Name of Geeks: " + this .name );
console.log( "No. of articles by Geeks: "
+ this .articles);
console.log( "No. of cp problems sol by Geeks: "
+ this .cp_problems)
}
}
var geek1 = new Geeks( "Abhinav" , 87, 560);
geek1.About();
|
Output:
Name of Geeks: Abhinav
No. of articles by Geeks: 87
No. of cp problems sol by Geeks: 560
The difference between interface and classes are below:
Interface
|
Classes
|
We can Create the interface with the use of the interface keyword.
i.e interface Interface_Name{ \\ Interface Body }
|
We can create the class with class keyword.
i.e class Class_Name{ \\ Class Body }
|
The interfaceblueprint is mainly the Type structure of object. i.e It is object with only defining the type of parameter inside. |
Class is the blueprint of the object i.e.the create purposes class is how we implement the object of our code. |
It is used for type checking purpose. Use of interface if TypeScript language is mainly focused on the checking the type of parameters in object. |
Classes in Types script is used to made the object for something. It is used for implementing the object. |
We cannot create the instance of interface with new in typescript. It means that we cannot create the copy of instance in Typescript. |
We can create a new instance of the class in TypeScript. It means that we can create the copy of class with new keyword. |
Interface is virtual structure. Means it only present in TypeScript code not in TypeScript compiled JavaScript code. |
It always exists in code after the compilation of TypeScript to JavaScript. |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...