Mastering Data Transformation with Angular Pipes

Mastering Interfaces and Classes: Encapsulation Made Simple

Learning Outcome

5

Implement Getters and Setters (Accessors)

4

Learn the concept of Encapsulation

3

Understand classes and objects

2

Learn syntax and usage of interfaces

1

Understand Interfaces and Classes in TypeScript

Analogy

Imagine a car manufacturing company where engineers create a design blueprint.

This blueprint is like an Interface.

The blueprint defines:

  • Number of wheels
  • Engine type
  • Car color

Analogy

When the car is actually built using the blueprint, it becomes a Class Object.

Similarly in TypeScript:

Interface → Defines structure

Class → Implements the structure

Object → Actual instance created from class

Why Interfaces and Classes Are Important

This leads to clean and maintainable applications.

In real-world applications:

Applications have multiple objects and complex structures

Data must be organized and protected

What is an Interface?

But it does not contain implementation logic.

An Interface defines the structure of an object.

interface Student {
  name: string;
  age: number;
}

This interface defines what a Student object should look like.

It specifies:

  • Properties

  • Methods

Interface Usage

Objects can implement interfaces.

interface Student {
  name: string;
  age: number;
}

let student1: Student = {
  name: "Rahul",
  age: 22
};

Benefits:

Ensures consistent object structure

Improves code readability

Helps with type safety

What is a Class & Object?

A Class is a blueprint used to create objects.

It contains:

  • Properties

  • Methods

  • Constructors

class Student {
  name: string;
  age: number;

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
}

Classes help organize code into logical units.

Objects

Here:

  • Student → Class
  • s1 → Object
let s1 = new Student("Rahul", 22);

An object is an instance of a class.

Understanding Encapsulation

Encapsulation means protecting data inside a class and controlling access to it.

Instead of directly accessing a variable, we use methods or accessors.

Benefits:

  • Prevents direct modification of data
  • Improves security
  • Maintains data integrity

Access Modifiers (Basic Idea)

Here balance cannot be accessed outside the class.

class BankAccount {
  private balance: number = 1000;
}

TypeScript provides access modifiers like:

 Getters (Accessor)

A Getter is used to read private data safely.

class BankAccount {
  private balance: number = 1000;

  getBalance() {
    return this.balance;
  }
}
let account = new BankAccount();
console.log(account.getBalance());

Usage:

Getter allows controlled access to private data.

Setters (Accessor)

A Setter is used to update data safely.

class BankAccount {
  private balance: number = 1000;

  setBalance(amount: number) {
    this.balance = amount;
  }
}

Setters allow controlled modification of data.

Usage:

account.setBalance(2000);

Summary

5

Getters and setters provide safe access to private variables

4

Encapsulation protects internal class data

3

Objects are instances of classes

2

Classes provide implementation and behavior

1

Interfaces define structure of objects

Quiz

Getters and Setters are used for:

A. Routing

B. API calls

C. Controlled access to data

D. UI rendering

Quiz-answer

Getters and Setters are used for:

A. Routing

B. API calls

C. Controlled access to data

D. UI rendering