Object Oriented Principles

There are five main things provided by any Object Oriented Design.

  • Classes and Objects
  • Inheritance
  • Encapsulation
  • Abstraction
  • Polymorphism

Classes and Objects

Class is a blue print which we create by observing a real world entity. Class is a abstract signature of properties and methods that belongs to a particular entity.

Objects are the real concrete things which executes the behavior specified by a class. All objects of a particular class share the common blueprint.

In the last section, we created a class called Player. Then we created an object called player1. All classes will have two types of members in them.

  • Properties
  • Methods

Using TypeScript we can easily create classes and respective objects with the syntax similar to the C++ styled programming languages. More details in the coming chapter.

Inheritance

Inheritance is a mechanism in which a class can extend the properties and methods of the existing class. Using this one can keep code DRY(Don't Repeat Yourself). This gives the advantage of re using the designed behavior. It is also helpful in composing properties of parent and child classes. Inheritance can be one of following in TypeScript. Inheritance in JavaScript is painful and TypeScript addresses this problem perfectly.

Encapsulation

Encapsulation is a process of binding both the properties and methods in a class. If a method exist in a class and not using any property, then it is useless in belonging in the class. In JavaScript prototype object is performing the encapsulation. The relation between property and method is vital in designing the behavior of class.

Abstraction

Abstraction is hiding the complex details and protecting properties which are only operated between methods. Only methods can access properties if properties are abstracted. It also stops the danger of modifying the internal properties of a class by code lying outside the class.

Polymorphism

Polymorphism is a process of having same functions but varied behavior depending on the type and number of arguments defined in the signature. Overloading and Generic programming are actually implementations of the polymorphism. Using polymorphism feature we can multiplex the program behavior.

All these features will be discussed with code in upcoming chapters.