Activity 10: Object-Oriented Programming OOP in TypeScript
Core OOP Concepts in TypeScript
Class and Object
A class is a blueprint or template for creating objects. It defines properties and methods that the objects created from the class will have.
An object is an instance of a class. It holds actual data and interacts with methods defined in the class.
Key Features:
Classes define the structure (properties and methods) of an object.
Objects are individual instances created from classes, each with its own data.
Implementation in TypeScript: TypeScript allows you to define classes using the class keyword. Objects can be created using the new keyword.
Example Code:
Encapsulation
Encapsulation is the concept of bundling the data (properties) and methods (functions) that operate on the data within one class, while restricting access to some of the details (hiding internal implementation).
Key Features:
Private: Only accessible within the class.
Protected: Accessible within the class and subclasses.
Public: Accessible from anywhere.
Implementation in TypeScript: TypeScript uses access modifiers (public, private, and protected) to control access to class members.
Example Code:
Inheritance
Inheritance allows one class (child or subclass) to inherit properties and methods from another class (parent or superclass).
Key Features:
Promotes code reuse by inheriting behavior from parent classes.
The
extendskeyword is used to establish inheritance.Subclasses can override methods from parent classes.
Implementation in TypeScript: TypeScript uses the extends keyword to inherit one class from another.
Example Code:
Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common parent class. It can be achieved through method overriding (runtime polymorphism) and method overloading (compile-time polymorphism).
Key Features:
Method Overriding: A subclass can provide a specific implementation for a method that is already defined in its superclass.
Method Overloading: Methods can have the same name but different parameter lists.
Example Code (Method Overriding):
Example Code (Method Overloading):
Abstraction
Abstraction is the process of hiding the implementation details and showing only the functionality to the user. It allows you to define methods without providing full implementation.
Key Features:
Abstract classes can have both implemented and abstract methods.
Interfaces are contracts that classes can implement.
Abstract Class Example:
Interfaces
An interface defines the structure of an object but does not provide implementation. It is used to define a contract that a class must adhere to.
Key Features:
Defines method signatures and properties without implementation.
Promotes loose coupling and flexibility.
Implementation in TypeScript:
Constructor Overloading
Constructor overloading allows you to define multiple constructors with different parameter types or numbers. TypeScript supports this using optional parameters or union types.
Key Features:
Define multiple ways to create an instance of a class.
Can handle different initialization scenarios.
Implementation in TypeScript:
Getters and Setters
Getters and setters allow you to control access to a class's properties by encapsulating how they are read or modified.
Key Features:
Provides controlled access to properties.
Can include validation or logic in accessors.
Implementation in TypeScript:
References:
https://www.typescripttutorial.net/typescript-tutorial/typescript-getters-setters/
https://www.typescriptlang.org/docs/handbook/classes.html
https://www.tutorialsteacher.com/typescript/data-modifiers
https://www.typescripttutorial.net/typescript-tutorial/typescript-inheritance/
https://stackoverflow.com/questions/12702548/constructor-overload-in-typescript