Object oriented programming (OOP) is a fundamental programming paradigm used by nearly every developer at some point in their career. OOP is the most popular programming paradigm and is taught as the standard way to code for most of a programmers educational career.
Today we will break down the basics of what makes a program object-oriented so that you can start to utilize this paradigm in your own projects and interviews.
What is Object Oriented Programming?
A class is an abstract blueprint used to create more specific, concrete objects. Classes often represent broad categories, like Car or Dog that share attributes. These classes define what attributes an instance of this type will have, like color, but not the value of those attributes for a specific object.
Classes can also contain functions, called methods available only to objects of that type. These functions are defined within the class and perform some action helpful to that specific type of object.
For example, our Car class may have a method repaint that changes the color attribute of our car. This function is only helpful to objects of type Car, so we declare it within the Car class thus making it a method.
Class templates are used as a blueprint to create individual objects. These represent specific examples of the abstract class, like myCar or goldenRetriever. Each object can have unique values to the properties defined in the class.
For example, say we created a class, Car, to contain all the properties a car must have, color, brand, and model. We then create an instance of a Car type object, myCar to represent my specific car.
We could then set the value of the properties defined in the class to describe my car, without affecting other objects or the class template.
We can then reuse this class to represent any number of cars.
Class blueprint being used to create two Car type objects, myCar and helensCar
Benefits of OOP
OOP models complex things as reproducible, simple structures
Reusable, OOP objects can be used across programs
Allows for class-specific behavior through polymorphism
Easier to debug, classes often contain all applicable information to them
Secure, protects information through encapsulation