OOPs Concepts in TypeScript. What is the 4, Basics of Object-Oriented Programming Fundamentals & their Examples

1. Abstraction

Abstraction lets programmers create useful and reusable tools. For example, a programmer can create several different types of objects, which can be variables, functions or data structures. Programmers can also create different classes of objects as ways to define the objects. For instance, a class of variable might be an address. The class might specify that each address object shall have a name, street, city and zip code. The objects, in this case, might be employee addresses, customer addresses or supplier addresses.

Abstraction Example:

abstract class Address 
    private street: string;
    private city: string;
    private codePostal: number;

    constructor(street: string, city: string, codePostal: number)
        this.street = street;
        this.city = city;
        this.codePostal = codePostal;

    public getFullAddress(): string 
        return `$this.street, $this.city, $this.codePostal`;

class FirstCustomer extends Address 
    constructor(street: string, city: string, codePostal: number)
        super(street, city, codePostal);

class SecondCustomer extends Address 
    constructor(street: string, city: string, codePostal: number)
        super(street, city, codePostal);

new FirstCustomer("234 Washigton Road", "Honolulu", 283930);
new SecondCustomer("456 Mandela Street", "New Jarsey", 58893);

2. Encapsulation

Encapsulation lets us reuse functionality without jeopardising security. It’s a powerful, time-saving OOP concept in TypeScript which related to Java. For example, we may create piece of code that calls specific data from a database. It may be useful to reuse that code with other databases or processes. Encapsulation lets us do that while keeping our original data private. It also lets us alter our original code without breaking it for others who have adopted it in the meantime.

Encapsulation Example:

class Customer 
    private name: string;
    private surname: string;

    public getFullName(): string 
      return `$this.name - $this.surname`;

    public setNames(name: string, surname: string): void 
      this.name = name;
      this.surname = surname;

class SetGetCustomer 
        const name: string = "Jon";
        const surname: string = "Snow";
        const customer: Customer = new Customer();
        customer.setNames(name, surname);
        const fullname: string = customer.getFullName();

new SetGetCustomer();

3. Inheritance

Inheritance is another labor-saving OOP concept that works by letting a new class adopt the properties of another. We call the inheriting class a subclass or a child class. The original class is often called the parent. We use the keyword extends to define a new class that inherits properties from an old class.

Inheritance Example

class ParentCustomer 
    private surname: string = "Scofield";
    public familyName(): string 
        return this.surname;

class ChildCustomer extends ParentCustomer 
    private name: string;
    constructor(name: string)
        this.name = name;
    public childFullName(): string 
        const surname: string = this.familyName();
        return `$this.name - $surname`;

new ChildCustomer("Aleah");
new ChildCustomer("Reagan");

4. Polymorphism

Polymorphism in TypeScript works by using a reference to a parent class to affect an object in the child class. We might create a class called “horse” by extending the “animal” class. That class might also implement the “professional racing” class. The “horse” class is “polymorphic,” since it inherits attributes of both the “animal” and “professional racing” class. Two more examples of polymorphism in Java are method overriding and method overloading.

In method overriding, the child class can use the OOP polymorphism concept to override a method of its parent class. That allows a programmer to use one method in different ways depending on whether it’s invoked by an object of the parent class or an object of the child class.In method overloading, a single method may perform different functions depending on the context in which it’s called. This means a single method name might work in different ways depending on what arguments are passed to it

Polymorphism Example

class CustomerPolymorphism 
    canComplaining(): void 
        console.log("The Customer is wrong on this matter");

class CustomerPolymorphismOverride extends CustomerPolymorphism 
    canComplaining(): void 
      console.log("Stop that, We y'all Customer is always right no matter what")
    public main(): void 

const customerPolymorphismOverride: CustomerPolymorphismOverride = new CustomerPolymorphismOverride();

Let’s us connect on linkedIn. profile name is: Reagan Scofield Mukandila

Source link