# Activity 10: Object-Oriented Programming OOP in TypeScript

**Class and Object:**

Class - A blueprint for creating objects.

Object - An instance of a class, with specific values for its properties.

**Explain how classes act as blueprints and how objects are instances of classes.**

In OOP, classes are like blueprints or templates for creating objects. They define the structure and behavior of a specific type of object. An object, on the other hand, is an actual instance of a class, a concrete realization of the blueprint.

**Show how to define a class and create an object in TypeScript.**

Imagine a class named "Car." This class might define properties like "color," "make," "model," and "year," as well as methods like "start," "stop," and "accelerate." When you create an object of the "Car" class, you are essentially creating a specific instance of a car, with its own unique values for these properties. You could create multiple car objects, each with different colors, makes, and models.

**Encapsulation:**

Encapsulation is the practice of bundling data (attributes) and methods (functions) that operate on that data within a single unit, known as a class. This encapsulation protects the data from direct access, ensuring data integrity and control.

**Demonstrate how TypeScript uses access modifiers like public, private, and protected to control access to class properties and methods.**

In TypeScript, encapsulation is achieved using access modifiers. Public members are accessible from anywhere, while private members are accessible only within the class itself. This restricted access protects data integrity and promotes modularity.

**Inheritance:**

Inheritance allows new classes (derived classes) to inherit properties and methods from existing classes (base classes), promoting code reuse and a hierarchical structure. This facilitates code extensibility and reduces redundancy.

**Demonstrate how to use the extends keyword and how to override methods from the parent class.**

“ Code Reusability” In TypeScript, inheritance is achieved using the "extends" keyword. Derived classes inherit properties and methods from their base classes, and they can override inherited methods or introduce new ones.

**Polymorphism:**

Polymorphism means "many forms". In OOP, it allows objects of different classes to be treated as objects of a common type. This allows for flexibility and extensibility, enabling you to write code that can handle different types of objects in a unified way.

**Show examples of both method overriding (runtime polymorphism) and method overloading (compile-time polymorphism) in TypeScript.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726834711128/5465f901-6c14-4e40-a236-feea303e9d20.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726834720041/d28c8bc0-ac43-4f88-be8f-0b4da51eb8f4.png align="center")

**Abstraction:**

Abstraction simplifies complex systems by focusing on essential features and hiding unnecessary details. In OOP, it involves representing a real-world entity with a simplified model, highlighting its key characteristics and behaviors. Abstraction promotes code reusability and maintainability.

**Demonstrate how to use abstract classes and interfaces in TypeScript to implement abstraction.**

“ Simplified Representation” TypeScript implements abstraction using abstract classes and interfaces. Abstract classes define a blueprint for a class, allowing for partial implementation, while interfaces define a contract that classes can implement.

**Additional OOP Concepts to Include:**

**Interfaces:**

Interfaces in TypeScript are blueprints that define the structure of an object. They outline the properties and methods that a class must implement. Interfaces do not contain implementation details, but they act as contracts that ensure consistent behavior across different classes.

**Show how interfaces help in achieving abstraction.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726834885861/28592a35-8d28-4206-9372-43c59f1214a2.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726834896364/0b6116a0-0ce4-44dd-a67c-20c6e1dc4d15.png align="center")

**Constructor Overloading:**

TypeScript allows you to define multiple constructors for a class, each with different parameters. This is called constructor overloading.

**Flexibility**

Constructor overloading provides flexibility in creating objects with different initial states.

**Optional Parameters**

TypeScript supports optional parameters, allowing you to provide different sets of information during object creation.

**Type Safety**

TypeScript enforces type safety during constructor calls, ensuring that the correct parameters are passed.

**Show an example of implementing constructor overloading.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726835121563/a774e7f9-8dbc-4ab7-ab25-a7a68a73edaa.png align="center")

**Getters and Setters:**

Getters and setters are methods that provide controlled access to properties within a class. They encapsulate the logic for reading and writing values.

**Getters**

Getters are methods that retrieve the value of a property. They are prefixed with the "get" keyword.

 **Setters**

Setters are methods that modify the value of a property. They are prefixed with the "set" keyword.
