Activity 11: Object-Oriented Programming OOP in TypeScript

What is OOP or Object-Oriented Programming?

  • Object-Oriented Programming (OOP) is a way of writing programs that focuses on objects. These objects stand for things from the real world and are built to hold both data (like characteristics or features) and actions (like functions or tasks). OOP helps to arrange and manage code so that it's easier to understand, change, and grow.

Here’s how features enhance Object-Oriented Programming (OOP) practices by making code more reliable, organized, and easier to maintain:

Encapsulation:

  • A fundamental idea of object-oriented programming (OOP) is encapsulation, which entails combining the methods (functions) that manipulate the data and the data itself (attributes) into a single unit called a class. It also limits access to parts of the object's components, which helps to prevent unwanted interference and data exploitation..

  • private : The property or method is only accessible within the class in which it is defined. It cannot be accessed or modified from outside the class.

  • public : The property or method is accessible from anywhere (inside or outside the class).

  • protected : The property or method is accessible within the class and by subclasses (derived classes), but not from outside the class.

INHERITANCE:

  • Permits a class to inherit the methods and properties of another class and creates new classes based on existing ones, allowing for code reuse and expansion.

    POLYMORPHISM:

    Polymorphism is an important notion in object-oriented programming (OOP) because it allows objects from various classes to be viewed as instances of the same class. Because various classes can have a same parent class or interface

    Two types of polymorphism:

  • Compile-time polymorphism (Method Overloading): This happens when multiple methods have the same name but different parameter lists within the same class. It is resolved during compile time based on the method signature.

  • Runtime polymorphism (Method Overriding): This occurs when a subclass provides a specific implementation of a method that is already defined in its parent class. It is resolved at runtime based on the actual object's type.

Example: Method Overriding (Runtime Polymorphism)

Method Overloading (Compile-time Polymorphism)

Abstraction:

An abstract class is a class that cannot be instantiated on its own and is meant to be extended by other classes. It may contain both abstract methods (methods without implementation) and concrete methods (methods with implementation).Abstract classes define the structure of the class, allowing subclasses to implement the abstract methods as needed.

Using Interfaces

Additional OOP Concepts to Include:

Interfaces:

  • An interface is a way to define the structure of an object or a class by specifying the properties and methods it should have. However, interfaces do not provide any implementation details; they only define what is required, leaving the actual implementation to the class or object that uses the interface.

Constructor Overloading:

  • In TypeScript, unlike some other languages, you cannot define multiple constructors with different signatures directly. However, TypeScript supports optional parameters and default parameters in a single constructor, allowing you to mimic multiple constructor behavior.

Example: Constructor with Optional Parameters

Example: Constructor with Default Parameters

Getters and Setters:

  • getters and setters provide a way to encapsulate and control the access to class properties. By using get and set methods, you can control how a property is accessed or modified, ensuring that the internal logic is preserved and only valid values are assigned to the properties.

Example: Defining Getters and Setters in TypeScript

for further reading and understanding, you may visit these sites below:

oop concepts - Bing. (n.d.). Bing. https://www.bing.com/search?q=oop+concepts&FORM=bngcht&toWww=1&redig=81505DE4904D4EDC9AE04A09B644C161

GeeksforGeeks. (2023, February 9). Introduction of object oriented programming. GeeksforGeeks. https://www.geeksforgeeks.org/introduction-of-object-oriented-programming/

Object-oriented programming - Learn web development | MDN. (2024, July 26). MDN Web Docs. https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_programming