Object Oriented Programming

Keith Williams
5 min readMay 22, 2021

--

Object Oriented Programming is a programming paradigm that is built upon classes and objects. In essence, it groups together data and functions that are related to one another. This allows for programs to be broken down into simple reusable components.

Classes and Objects

The two major concepts of Object Oriented Programming are classes and objects. Basically a class is a specific group and an object is a particular instance of that group. For example, say that a school wanted to create of database of its students. Each student would have a list of attributes (name, year, GPA, major). The class would be a blueprint of how to create the profile for each student. Each student that we add into the database would be considered an object.

Above is an example of how to create a class in JavaScript. We define the class name (Student). Then we use a constructor to assign the attributes to that specific object (or instance) of the student class. We use the “this” keyword which is saying “assign these attributes to this specific instance of the class.” In the bottom line we are creating a new object named “student1” by assigning it to a variable and passing the arguments into a new instance of the student class.

We can also define functions within the class which would allow us to call those functions on any instance of that class. For example, if we wanted to create a function called “honors” which determines if the GPA of a specific student is above 3.7 we could just add it into the class.

Now the function “honors” can be called on any object of the Student class, as shown in the bottom line.

The Four Pillars

There are four principles of Object Oriented Programming. They are Encapsulation, Abstraction, Polymorphism and Inheritance.

Encapsulation

The principle of encapsulation is hiding data within a class and restricting access to it. Class functions can be used on the class objects to return a value, but the actual logic written within the function is not accessible outside of the class. You can also control which information you would like to make public or private. If we wanted to make a class attribute or function private in JavaScript we could declare them using a hash (#).

In this case we are unable to access the name of “student1.” If you are using the hash to make an attribute private, you have define it in the class before the constructor, as shown above.

Abstraction

Abstraction is similar to encapsulation. Abstraction is only showing the essential data and keeping the rest private. We can retrieve data from a class and use its functions on the class’s objects but we don’t have access to the implementation of that code. This is meant to simplify the program and also increase security. By keeping logic and data contained within classes, it prevents larger programs from becoming too entangled or complex. Also, if we need to make a change to a specific class we don’t have to worry about the change affected other classes.

Inheritance

Inheritance allows data to be passed down from parent classes to child classes. Inside of our classes we can create subdivisions of that class.

In this case we created a Parent class and a Child class as a subdivision. Because the Child class is a child of the Parent class, it inherits its attributes and functions. When we create a new Child object, it uses the constructor of the Parent class to assign the name attribute. That object now inherits the functions contained in the Parent class as well as the functions in the Child class.

Polymorphism

Polymorphism allows the same method to execute differently. There are two different ways to do this. Method Overloading(static polymorphism) and method overriding(dynamic polymorphism). With method overloading, the function behaves differently based on the arguments passed into it.

In this example in Java, we have a method “tripFunc” that is written for both one argument and two arguments. The number of arguments passed in will determine which version of the function gets executed.

With method overriding, we can have two different methods with the same name. One in the parent class and one in the child class. If an object gets created in the child class it will inherit the child method version. If it is created in the parent class and is not in the child subcategory, then it will inherit the parent version.

In this example, both the Student class and the Athlete class have a “statement” function. The Athlete class is a child of the Student class so it inherits it’s functions, but because it has it’s own function with the same name, that overrides the Student class function for objects of the Athlete class. This can help eliminate the need to have several conditional statements written within the original function.

--

--

No responses yet