Study

Object Oriented Programming Principles

Credits from:

Before objects, we had primitive data types that stores single, simple values (eg. Byte, Int, Boolean, Float, Double, Char). But, we need more complex data types to store values… For example, in a chess game, for each piece - eg. knight - we need to store the position, ifCaptured, color, movement etc.. It would be easier if we group all those values together as one entity. Additionally, we could group some knights together as they behave similarly.

In C or C++ programming language, we have:

Structures Arrays
- Stores many pieces of data - Stores many pieces of data
- CAN store different types of data (eg. int and strings together) - CANNOT store different types of data
- Can store other structures within it  
- CANNOT store functions within one !!! (unlike objects)  

The structures are the precursor of the objects.


Definitions:

In Object Oriented Programming, we combine groups of related variables and functions into objects. We refer to variables as properties and to functions as methods within an object.

Encapsulation

Encapsulation refers to bundling data with methods that can operate on that data within a class.

Abstraction

Abstraction refers to only showing the essential details and hiding everything else. As long as we understand the outcome, the process is not very important.

Users of the classes created should not worry about the inner details of those classes.

It’s best to think of a program (big project) in terms of an Interface and Implementation

Inheritance

Inheritance is the principle that allows classes to derive from other classes.

With inheritance, we will have a class hierarchy that acts as a web of classes with different relationships to one another.

Access modifiers change which classes have access to other classes, methods or attributes. There are 3 types of access modifiers:

(members = properties/variables of a class and/or methods/functions of a class)

Inheritance will also help in eliminating redundant code (we will not have to redefine properties and methods for each different object… these objects could be simply be inherited from a generic object).

Polymorphism

Polymorphism describes the methods that are able to take on many forms.

There are two types of polymorphism:

(subclasses could also be called: extended class or child class; superclasses could also be called parent class or base class)


JavaScript OOP