Developer Dictionary: Object Oriented Programming
Tags: beginners, computerscience, codenewbie, firstyearincode
Published: 2020-09-02 03:11:31

**This post is part of a resource called Developer Dictionary. More about why I'm writing this [here](https://bhumimakes.com/developer-dictionary/)** 

**[Sign-up for new entries](https://mailchi.mp/bb86c0c65a49/connectingdots)**

***

Programming is about solving real world problems. The first step of a solution is to represent the real world within the digital world of a computer in some way. So that we can begin to model the problem and solution. 'classes' and 'objects' are a way to do that. 

###What is The Thing:

Object Oriented programming (OOP) is a programming paradigm — a way of thinking about things — where we model things with 'objects'. Objects can be thought to contain two things — data and code. Data is the information unique to that object and code is for doing something with the data, manipulating the data in some way. 

###Example:
Imagine you're programming a game that has different shapes. The user can choose different colors for each shape, can move the shape around on screen, and change its size. How can we model that with classes and objects?

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p67eyjummwytdyufpvsm.jpg)

We can have a class called Shape, we can have other classes called Triangle, Rectangle, and Hexagon. The later three classes are all shapes, they are a type of shape. So we say that Shape is the super class, and other three are sub classes.

The class Shape has two attributes called color and location. Imagine color could be "purple", "green", "orange", etc. The location attribute is interesting. It is meant to represent where the shape is located on screen using X and Y coordinates. We manage this by creating another class called Point and the main job of that class is to hold the two attributes X and Y.

Class Shape also has two methods — move() and changeColor(). The move() method, we can imagine, takes a Point as a parameter and sets the location to that new point. The changeColor() method similarly takes a string as a parameter and sets the color attribute to the new value.

The class Triangle , Rectangle, and Hexagon are subclasses of the Shape class. So they will all 'get' the two attributes and two methods from Shape. In addition, Triangle has a size attribute and a changeSize() method. Hexagon has the same two as well. Rectangle has length and width attributes and methods changeLength() and changeWidth().

With these classes, attributes and methods we have modeled the simple game we wanted. Users can change color, change location, and change size of a few shapes.
 
Notice that we have made some assumptions — for example, we assume that all sides of the triangle and hexagon are equal. In this model we have not allowed for modeling triangles where each of the 3 sides might be different. And also our model is good but not comprehensive — it does not include all the possible shapes, or it does not allow for changing individual sizes, etc.

###Key Ideas and Terminology

There are many sub-concepts within the world of OOP. We will review the most foundational ones here.

So what is different about objects and their methods? The methods act on the object. This is different from having a 'function', for example, that can add two numbers and take both of those numbers as parameter. So sum(num1, num2) vs. num1.sum_oop(num2). The latter sum_oopis 'object-oriented'. In this case sum_oop is a method of num1's class. And it takes num2 as a parameter and adds it to num1.

-- Everything is an object of a class. All objects are instantiated from a class definition. class definition is like a template for the object.

-- A Class consists of attributes (aka properties, fields) and methods (aka operations). You can think of attributes as data and methods as code to do something with the data.

-- The fields or attributes of a class are kept 'private' to the class. Any users of the class needs to use methods to access the field. This idea is called encapsulation of the internal state.

-- Constructors - are special methods that are used to create or instantiate objects of a class.
 
-- Instance methods vs. class methods - class methods can be called directly on the class. Instance methods are methods that require and object of a class.

###Does JavaScript use OOP?

Sort of. But not exactly. JavaScript uses something called Prototypical Inheritance. In prototype-based languages the objects are the primary things. No classes are defined per se. The prototype of an object is like a template for creating the object.

***

The concept of OOP has been in use for a while. Terminology invoking "objects" and "oriented" first appeared in the late 1950s and early 1960s. Byte Magazine's August 1981 issue introduced a programming language called Smalltalk and object-oriented programming to a wider audience. In 1986 the first Conference around OOP was unexpectedly attended by 1,000 people.

Now you know the essence of the Object Oriented Programming. But you now concept of classes, objects, attributes, methods, constructors, encapsulation, instance vs. class.