OOPS, which stands for Object-Oriented Programming (OOP), is a programming paradigm that focuses on organizing code into objects, which are instances of classes. It aims to structure software systems based on real-world objects, their properties, and their interactions.
The key concepts of OOP include:
Classes and Objects: A class is a blueprint or template that defines the structure and behavior of objects. An object is an instance of a class that encapsulates data and methods related to that class.
Encapsulation: Encapsulation refers to the bundling of data and methods within a class, hiding internal details and providing a well-defined interface for interacting with the object. It helps in achieving data abstraction and information hiding.
Inheritance: Inheritance allows the creation of new classes based on existing classes. The new class (subclass or derived class) inherits the properties and methods of the existing class (superclass or base class) and can extend or modify them. Inheritance promotes code reusability and supports the "is-a" relationship between classes.
Polymorphism: Polymorphism means the ability of objects of different classes to respond to the same message or method call in different ways. It allows different objects to be treated uniformly based on their common interface or superclass, while their specific implementation is determined at runtime.
Abstraction: Abstraction focuses on capturing the essential features of an object or a class while ignoring unnecessary details. It allows programmers to create abstract classes and interfaces that define the common structure and behavior for a group of related objects.
Association: Association represents a relationship between two or more objects, indicating that they are connected in some way. It can be a simple "has-a" relationship or a more complex association like aggregation or composition.
Polymorphism: Polymorphism means the ability of objects of different classes to respond to the same message or method call in different ways. It allows different objects to be treated uniformly based on their common interface or superclass, while their specific implementation is determined at runtime.
These concepts help in creating modular, reusable, and maintainable code, making OOP a popular approach for developing software systems. It provides a structured and organized way to model complex systems by focusing on objects and their interactions.
Sure! Here are some examples of OOP concepts implemented in TypeScript:
- Classes and Objects:
typescriptclass Car {
private brand: string;
private model: string;
constructor(brand: string, model: string) {
this.brand = brand;
this.model = model;
}
public startEngine(): void {
console.log(`Starting the engine of ${this.brand} ${this.model}`);
}
}
const myCar = new Car("Toyota", "Camry");
myCar.startEngine();
- Encapsulation:
typescriptclass BankAccount {
private accountNumber: string;
private balance: number;
constructor(accountNumber: string) {
this.accountNumber = accountNumber;
this.balance = 0;
}
public deposit(amount: number): void {
this.balance += amount;
}
public withdraw(amount: number): void {
if (amount <= this.balance) {
this.balance -= amount;
} else {
console.log("Insufficient balance.");
}
}
public getBalance(): number {
return this.balance;
}
}
const myAccount = new BankAccount("123456789");
myAccount.deposit(1000);
myAccount.withdraw(500);
console.log("Current balance:", myAccount.getBalance());
- Inheritance:
typescriptclass Animal {
protected name: string;
constructor(name: string) {
this.name = name;
}
public makeSound(): void {
console.log("Animal is making a sound.");
}
}
class Dog extends Animal {
public makeSound(): void {
console.log("Dog is barking.");
}
}
const myDog = new Dog("Buddy");
myDog.makeSound();
- Polymorphism:
typescriptclass Shape {
public area(): number {
return 0;
}
}
class Circle extends Shape {
private radius: number;
constructor(radius: number) {
super();
this.radius = radius;
}
public area(): number {
return Math.PI * Math.pow(this.radius, 2);
}
}
class Rectangle extends Shape {
private width: number;
private height: number;
constructor(width: number, height: number) {
super();
this.width = width;
this.height = height;
}
public area(): number {
return this.width * this.height;
}
}
const shapes: Shape[] = [new Circle(5), new Rectangle(4, 6)];
shapes.forEach((shape) => {
console.log("Area:", shape.area());
});
These examples illustrate the usage of OOP concepts in TypeScript, demonstrating class creation, encapsulation of data and methods, inheritance, and polymorphism.