Object Oriented Programming in TypeScript
- Get link
- X
- Other Apps
Object Oriented Programming in TypeScript
Object Oriented Programming (OOP) is a programming paradigm that organizes software design around objects, which contain both data (properties) and behavior (methods). TypeScript, a superset of JavaScript developed by Microsoft, fully supports object-oriented concepts such as classes, inheritance, polymorphism, encapsulation, and abstraction.
TypeScript brings strong typing and advanced object-oriented features to modern web development, making code more structured, reusable, and easier to maintain.
What is TypeScript?
TypeScript is an open-source programming language that builds on JavaScript by adding static types, classes, and interfaces. It compiles into plain JavaScript and runs in any environment where JavaScript runs, such as browsers or Node.js.
Developers prefer TypeScript because it helps detect errors during development and improves code readability.
Key OOP Concepts in TypeScript
1. Classes and Objects
A class is a blueprint for creating objects. An object is an instance of a class.
Example
class Student {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
display(): void {
console.log("Name: " + this.name);
console.log("Age: " + this.age);
}
}
let student1 = new Student("Ravi", 21);
student1.display();
Explanation:
class Studentdefines a class.constructorinitializes object properties.display()is a method that prints data.
2. Encapsulation
Encapsulation means hiding internal data and controlling access using access modifiers.
TypeScript provides three access modifiers:
publicprivateprotected
Example
class BankAccount {
private balance: number;
constructor(balance: number) {
this.balance = balance;
}
public showBalance(): void {
console.log("Balance: " + this.balance);
}
}
let acc = new BankAccount(5000);
acc.showBalance();
Here, balance is private and cannot be accessed outside the class.
3. Inheritance
Inheritance allows a class to reuse properties and methods from another class.
Example
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
display(): void {
console.log("Name: " + this.name);
}
}
class Teacher extends Person {
subject: string;
constructor(name: string, subject: string) {
super(name);
this.subject = subject;
}
showSubject(): void {
console.log("Subject: " + this.subject);
}
}
let t = new Teacher("Sita", "Math");
t.display();
t.showSubject();
Here, Teacher inherits properties from Person.
4. Polymorphism
Polymorphism means one function behaving differently depending on the object.
Example
class Animal {
makeSound(): void {
console.log("Animal makes sound");
}
}
class Dog extends Animal {
makeSound(): void {
console.log("Dog barks");
}
}
let a = new Dog();
a.makeSound();
The same method behaves differently for different classes.
5. Abstraction
Abstraction means hiding implementation details and showing only functionality.
TypeScript supports abstraction using abstract classes.
Example
abstract class Shape {
abstract area(): number;
}
class Circle extends Shape {
radius: number;
constructor(radius: number) {
super();
this.radius = radius;
}
area(): number {
return Math.PI * this.radius * this.radius;
}
}
let c = new Circle(5);
console.log(c.area());
Here, the abstract class Shape defines the structure but not the implementation.
Advantages of OOP in TypeScript
Code Reusability – Classes can be reused through inheritance.
Better Maintainability – Structured code is easier to manage.
Improved Security – Encapsulation protects data.
Scalability – Large applications can be developed efficiently.
Error Detection – Type checking reduces runtime errors.
Conclusion
Object Oriented Programming in TypeScript helps developers build robust, scalable, and maintainable applications. By using concepts like classes, inheritance, encapsulation, polymorphism, and abstraction, developers can write cleaner and more organized code. As modern web development continues to grow, TypeScript and OOP play a crucial role in creating high-quality.
- Get link
- X
- Other Apps
Comments
Post a Comment