Difference between interfaces and classes in TypeScript
Last Updated :
16 May, 2024
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 for Class
interface ForClass {
readonly var1:string;
}
let newclass: ForClass = {var1:"Interface"};
console.log(newclass);
Output:
{ var1: 'Interface' }
Example 2:
JavaScript
// Interface for Object with extends function
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:
JavaScript
class Geeks {
name : string ;
articles: number ;
cp_problems: number;
// Constructor of class
constructor(name:string, articles: number, cp_problems: number) {
this.name = name;
this.articles = articles;
this.cp_problems = cp_problems;
}
// About object
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. |
Similar Reads
What is the difference between interface and type in TypeScript ? In TypeScript, both interface and type are used to define the structure of objects, but they differ in flexibility and usage. While interface is extendable and primarily for object shapes, type is more versatile, allowing unions, intersections, and more complex type definitions.Type in TypeScriptThe
3 min read
TypeScript Differences Between Type Aliases and Interfaces Type In TypeScript, both type aliases and interfaces are used to define custom types, but they have distinct differences and use cases.Type Aliases: Allow the definition of types with a custom name, applicable to primitives, unions, tuples, and more complex types. Interfaces: Primarily used for defining
4 min read
Difference between Flow and TypeScript 1. Flow : Flow is developed and maintained by Facebook. It is a static type checker, designed to quickly find errors in JavaScript applications. Nothing more, nothing less. It's not a compiler, but a checker. It can work without any type of annotations and it is very good at inferring types. To enab
2 min read
Difference Between Package and Interface in Java In Java, packages and interfaces play crucial roles in organizing and structuring code. They serve different purposes and are used in distinct contexts. In this article, we will learn the concepts of the packages and interfaces in Java. syntax provides examples for each and then presents a table hig
2 min read
Difference Between valueof and keyof in TypeScript In TypeScript, valueOf() retrieves the primitive value of an object, useful for accessing inherent data types. Conversely, keyof extracts keys as a union type, crucial for type-safe property referencing and dynamic type handling in TypeScript's type system.ValueOf() methodThe valueOf() method is a b
2 min read
Difference between TypeScript and JavaScript Ever wondered about the difference between JavaScript and TypeScript? If you're into web development, knowing these two languages is super important. They might seem alike, but they're actually pretty different and can affect how you code and build stuff online.In this article, we'll break down the
4 min read