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?
Object Oriented programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects. There are many object-oriented programming languages including JavaScript, C++, Java, and Python.
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.
What is Object Oriented Programming? OOP Explained in Depth
Apr 15, 2020 - 15 min read
Erin Doherty
editor-page-cover
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?
Object Oriented programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects. There are many object-oriented programming languages including JavaScript, C++, Java, and Python.
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.
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.
Grouping related information together to form a class structure makes the code shorter and easier to maintain.
In the dogsitting example, here’s how a programmer could think about organizing an OOP:
Create a parent class for all dogs as a blueprint of information and behaviors (methods) that all dogs will have, regardless of type.
Create child classes to represent different subcategories of dog under the generic parent blueprint.
Add unique attributes and behaviors to the child classes to represent differences
Create objects from the child class that represent dogs within that subgroup
The diagram below represents how to design an OOP program: grouping the related data and behaviors together to form a simple template then creating subgroups for specialized data and behavior.
The Dog class is a generic template, containing only the structure about data and behaviors common to all dogs.
We then create two child classes of Dog, HerdingDog and TrackingDog. These have the inherited behaviors of Dog (bark()) but also behavior unique to dogs of that subtype.
Finally,we create objects of the HerdingDog type to represent the individual dogs Fluffy and Maisel.
We can also create objects like Rufus that fit under the broad class of Dog but do not fit under either HerdingDog or TrackingDog.
Building blocks of OOP:
Next, we’ll take a deeper look at each of the fundamental building blocks of an OOP program used above:
Classes
Objects
Methods
Attributes
Top comments (0)