object-oriented programming

An Introduction to Object-Oriented Programming

August 8, 2019 - Emily Newton

Revolutionized is reader-supported. When you buy through links on our site, we may earn an affiliate commision. Learn more here.

If you have any familiarity with the basics of coding languages, you’ve probably run across the term “object-oriented programming” (OOP) before. If you haven’t, you definitely will. When programmers sit down for job interviews, small-talk like “Describe your biggest weakness” quickly gives way to practical questions such as, “What are the main principles of object-oriented programming?”

Let’s answer this question today so you can be confident when you apply to your first entry-level developer job. Ready to dive in?

The Basics of Object-Oriented Programming

The object-oriented model is one where the program focuses on the data itself — the objects — rather than the underlying logical functions. Objects are data fields with their own known behaviors and discrete attributes. Traditionally, programming focused on the implementation of logic to determine how they interact, rather than first defining the information. Think of it like this:

Objects contain known behaviors and states, as well as the functions required to operate on the data within. Coding with OOP gives developers a way to have each of the separate objects within a program collaborate. There’s no need to check or manipulate the information within other objects. The only way for one object to affect another is through its known methods and functions. The programmer doesn’t have to define the logic.

Understanding classes and objects is more straightforward. If your name is Pat, you represent one object (or instance) of class — a human being. Each of us is an object within this class. We have a set of properties that includes name, address, age, phone number and more. Here, the object represents a real, physical thing in the world. Its class functions as a sort of broader logical definition.

Object-oriented programming begins with data modeling. The programmer identifies each object within a program and what needs to happen for proper communication. Once identified, it gets a class and logical sequence — that is, a method — for interacting with it.

Let’s put this all together:

Your Ford Fiesta is an object within the class of car. Its attributes include weight, color, maximum occupancy and more. The vehicle demonstrates various behaviors resulting from the methods associated with it. Examples include, “turn the key,” “step on the gas” and “make a right turn.” The class, car, provides a blueprint by which the object, Ford Fiesta, may be generated and then inherit some, but potentially not all, of the class attributes.

An object can inherit its behaviors and methods or it can have its own, through a process known as polymorphism. This is one of the four main principles within object-oriented programming.

What Is OOP Good For?

At its simplest, object-oriented programming lets developers focus their time and attention on objects rather than starting with the logic needed to manipulate them. This is an advantage when the work involves complex programs, or which receive regular code updates.

Another advantage of object-oriented programming is that it offers a more collaborative method during the development cycle. It’s easier to separate a project into separate tasks or groups. Building with object-oriented programing also means you can reuse portions of your code more frequently and scale your program up more easily and efficiently.

As we examine the four main components of object-oriented programming, we’ll explore some of these ideas in a bit more detail.

The Main Principles of Object-Oriented Programming

You won’t get very far without a rock-solid understanding of the four main principles of object-oriented programming. Here they are:

1. Abstraction

Abstraction is an answer to the problem of maintaining huge codebases for years at a time without confusing or losing the changes made along the way.

Developers use abstraction to make their lives easier. It offers a way for each object within the program to reveal only the higher-order mechanisms and functions for interacting with or using it. In other words, abstraction tells the program to focus on how objects must interface with one another, and removes the distraction of internal details.

Owning a cellphone is a bit like implementing abstraction: The interface is simple, but the stuff happening under the hood definitely isn’t. You don’t need to know how it works to use it successfully.

2. Encapsulation

A program defines the rules that multiple objects use to talk with one another. Encapsulation provides a way for each object to keep its individual state private within its class. The other objects can call upon functions to interact with these private objects, but do not have direct access.

To picture this, think of a barking dog. The dog’s bark is its method, but it might correspond to any number of individual private states — such as hungry, worried or tired. Based on the bark, a nearby human could interact with that dog by feeding it, petting it or placing it in its crate for the night. The private state of the dog and the public methods used by its human caretakers have been joined through encapsulation.

3. Inheritance

Inheritance is where a new object receives traits from an existing parent class. There’s more to it, though: Sometimes these new objects are mostly similar, but not quite identical. Inheritance also lets programmers reuse the logic, or code, that is common among all the classes they are working with.

For instance, if we were programming a cat or dog using the class human, we would allow the animal to inherit common features like eyes and ears and common behaviors like walking and respiration.

4. Polymorphism

Polymorphism is what allows a programmer to change how an object functions. For example, we can use it to make child classes behave more like the parent class from which they inherited traits. Polymorphism comes in two flavors: overload and override. Overload is like walking on your hands while everyone else uses their legs. Overriding is like using your legs to use a silly gate.

You’re Ready to Learn Even More

This has only been an introduction to object-oriented programming, but we hope it opens doors for you. There’s a lot more to see and learn. However, this takes care of one of the most important fundamentals in the programming and development worlds. It won’t take long to find free and useful OOP exercises and tutorials online so you can get some hands-on experience. What are you waiting for?

Revolutionized is reader-supported. When you buy through links on our site, we may earn an affiliate commision. Learn more here.

Author

Emily Newton

Emily Newton is a technology and industrial journalist and the Editor in Chief of Revolutionized. She manages the sites publishing schedule, SEO optimization and content strategy. Emily enjoys writing and researching articles about how technology is changing every industry. When she isn't working, Emily enjoys playing video games or curling up with a good book.

Leave a Comment