Private Members

Private members are only accessible from within the class they are declared. They cannot be accessed from outside the class. There are also not accessible from any subclasses that inherit them.

class Animal {
  private weight: number;
  private height: number;

  constructor(weight: number, height: number) {
    this.weight = weight;
    this.height = height;
  }
}

let animal = new Animal(1, 2);
console.log(animal); // Animal {weight: 1, height: 2}

Now if I want to access the weight or height property from outside the class, Typescript will throw an error.

animal.weight; // Property 'weight' is private and only accessible within class 'Animal'.
animal.height; // Property 'height' is private and only accessible within class 'Animal'.

However, the private members can be accessed from within a class.

class Animal {
  private weight: number;
  private height: number;

  constructor(weight: number, height: number) {
    this.weight = weight;
    this.height = height;
  }

  getWeight() {
    console.log(this.weight);
  }

  getHeight() {
    console.log(this.height);
  }
}

let animal = new Animal(1, 2);
console.log(animal); // Animal {weight: 1, height: 2}

animal.getWeight(); // 1
animal.getHeight(); // 2

As you can see, we have declared two methods getWeight() and getHeight(). Both methods can access the private members weight and height because they are declared within the class Animal.

Just like the fields, we can also declare methods as private.

class Animal {
  private weight: number;
  private height: number;

  constructor(weight: number, height: number) {
    this.weight = weight;
    this.height = height;
  }

  private getWeight() {
    console.log(this.weight);
  }

  private getHeight() {
    console.log(this.height);
  }
}

let animal = new Animal(1, 2);
console.log(animal); // Animal {weight: 1, height: 2}

animal.getWeight(); // Property 'getWeight' is private and only accessible within class 'Animal'.

animal.getHeight(); // Property 'getHeight' is private and only accessible within class 'Animal'.

Let’s access the private methods from within the class using a public method that calls both private methods.

class Animal {
  private weight: number;
  private height: number;

  constructor(weight: number, height: number) {
    this.weight = weight;
    this.height = height;
  }

  private getWeight() {
    return this.weight;
  }

  private getHeight() {
    return this.height;
  }

  getWeightHeight() {
    console.log(`weigth: ${this.getWeight()} height: ${this.getHeight()}`);
  }
}

let animal = new Animal(1, 2);
console.log(animal); // Animal {weight: 1, height: 2}

animal.getWeightHeight(); // weigth: 1 height: 2

It is important to note that any subclass that extends the Animal class does not have access to private members.

class Animal {
  private weight: number;
  private height: number;

  constructor(weight: number, height: number) {
    this.weight = weight;
    this.height = height;
  }

  private kingdom() {
    console.log("I belong to Animal Kingdom");
  }
}

class Dog extends Animal {
  info() {
    this.weight; // Property 'weight' is private and only accessible within class 'Animal'.
    this.kingdom(); // Property 'kingdom' is private and only accessible within class 'Animal'.
  }
}

We need to use the protected access modifier if we want to access these properties.

Protected members

Protected members are accessible from within the class they are declared and are also accessible from any subclasses that inherit them. However, cannot be accessed from outside the class.

class Animal {
  protected weight: number;
  protected height: number;

  constructor(weight: number, height: number) {
    this.weight = weight;
    this.height = height;
  }
}

let animal = new Animal(1, 2);
console.log(animal); // Animal {weight: 1, height: 2}

Now if I want to access the weight or height property from outside the class, Typescript will throw an error.

animal.weight; // Property 'weight' is protected and only accessible within class 'Animal' and its subclasses
animal.height; // Property 'height' is protected and only accessible within class 'Animal' and its subclasses

However, the protected members can be accessed from within a class.

class Animal {
  protected weight: number;
  protected height: number;

  constructor(weight: number, height: number) {
    this.weight = weight;
    this.height = height;
  }

  getWeight() {
    console.log(this.weight);
  }

  getHeight() {
    console.log(this.height);
  }
}

let animal = new Animal(1, 2);
console.log(animal); // Animal {weight: 1, height: 2}

animal.getWeight(); // 1
animal.getHeight(); // 2

As you can see, we have declared two methods getWeight() and getHeight(). Both methods can access the protected members weight and height because they are declared within the class Animal.

Just like the fields, we can also declare methods as protected.

class Animal {
  protected weight: number;
  protected height: number;

  constructor(weight: number, height: number) {
    this.weight = weight;
    this.height = height;
  }

  protected getWeight() {
    console.log(this.weight);
  }

  protected getHeight() {
    console.log(this.height);
  }
}

let animal = new Animal(1, 2);
console.log(animal); // Animal {weight: 1, height: 2}

animal.getWeight(); // Property 'getWeight' is protected and only accessible within class 'Animal' and its subclasses.
animal.getHeight(); // Property 'getHeight' is protected and only accessible within class 'Animal' and its subclasses.

Let’s access the protected methods from within the class using a public method that calls both private methods.

class Animal {
  protected weight: number;
  protected height: number;

  constructor(weight: number, height: number) {
    this.weight = weight;
    this.height = height;
  }

  protected getWeight() {
    return this.weight;
  }

  protected getHeight() {
    return this.height;
  }

  getWeightHeight() {
    console.log(`weigth: ${this.getWeight()} height: ${this.getHeight()}`);
  }
}

let animal = new Animal(1, 2);
console.log(animal); // Animal {weight: 1, height: 2}

animal.getWeightHeight(); // weigth: 1 height: 2

It is important to note that any subclass that extends the Animal class can access protected members.

class Animal {
  protected weight: number;
  protected height: number;

  constructor(weight: number, height: number) {
    this.weight = weight;
    this.height = height;
  }

  protected kingdom() {
    console.log("I belong to Animal Kingdom");
  }
}

class Dog extends Animal {
  myWeight() {
    console.log(this.weight);
  }
  myKingdom() {
    this.kingdom();
  }
}

let dog = new Dog(1, 2);

console.log(dog); // Dog {weight: 1, height: 2}

dog.myWeight(); // 1
dog.myKingdom(); // I belong to Animal Kingdom

As you can see, unlike private members, protected members weight and kingdom() properties of the Animal class are accessible from within the Dog class.