| |

Start Here with Object-Oriented Programming (OOP)

There are dozens of types of programming languages out there, and they all have their place in this big, wide world of software development. However, one of the most widely used type of languages is object-oriented programming (OOP). Put simply, these are the languages whose logic is designed based on their data or objects rather than functions. They do use functions in their logic quite extensively, but what lies at the foundation is how they handle data. Before we dive deeper into it, I think it could be helpful to talk first about what defines an object.

What is an object in OOP?

Think about an object as a noun. It is a thing that can have many characteristics, and it can behave a certain way. This is best understood by framing it with an example that make sense for programming logic.

Example: A book as an object

In this example, let’s say you’re designing some software for a small bookstore in your neighborhood. Obviously, one of the most important functions of this software is the ability to sell their books. Within that scope, they’d like to create a database of books they can custom order for their local customers. For this functionality, they will need to know some things about the book. (I’ll use a C# class to show you what the code for this object would look like.)

public class Book 
{
string title { get; set; }
string author { get; set; }
string category { get; set; }
int published { get; set; }
bool canOrder { get; set; }
int inventory { get; set; }
}

So now, when you declare this class at the beginning of the code, it essentially treats this class as a detailed data type. You can instantiate several objects in the main program. One way to do this would be the following:

Book book00001 = new Book();
book00001.title = “The Book Thief”;
book00001.author = “Markus Zusak”;
book00001.category = “Literary Fiction”;
book00001.published = 2005;
book00001.canOrder = true;
book00001.inventory = 5;

Note: This obviously is not the most concise way to instantiate this object, but it’s useful in learning exactly what an object is and how you can use it.

Finishing out this example, let’s say you want employees to be able to find the book in the store if it’s available. Its attributes are great for helping us do that. Here’s some code that would accomplish that:

if (this.inventory > 0)
{
Console.WriteLine($"{this.title} can be found in the {this.category} section.”);
}
else if (this.inventory < 1 && this.canOrder)
{
Console.WriteLine($"You need to order more copies of {this.title}.”);
else 
{
Console.WriteLine($"{this.title} is out of print.”);
}

Note: This code would be defined as a function within the class so it could be called on each book instantiated. This is what’s known as behavior of an object.

Foundational principles of OOP

There are four core principles of OOP that you’ll need to understand as a software developer. Let’s explore them below.

1—Encapsulation

When you’re working with classes in OOP, you can define their encapsulation. With encapsulation, you use access modifiers to control what parts of your code can access that particular class. Here’s a breakdown of the different access modifiers?

Private

The private keyword limits access to the class the object is defined in. Even the child classes wouldn’t have access to it. You can use this when you define child classes that you don’t want any other parts of the program accessing. 

Public

The public keyword doesn’t limit access at all. Any part of the program can access this class—as well as any other assembly if you’re using it to create a library. 

Protected

The protected keyword is similar to private, but it’s not identical. A protected class can be accessed by either the same class or a derived class. 

Internal

The internal keyword limits access to the current assembly. It acts similarly to the public keyword within that specific assembly. However, any other assembly that attempts to access it will receive an error.

You can also check out the following Microsoft chart that gives you a breakdown of the differences between these access modifiers (Note that there are a couple of combinations you can use to customize your encapsulation further):

Source: Microsoft (see resources at the end of this article)

2—Abstraction

This OOP concept says that there are parts of the program whose function don’t need to be understood by the entire program to function. When you look at the flow of a program, many of the tasks performed will be done with multiple classes and functions that the main program doesn’t spell out in its code. In other words, it’s hidden within those other classes and functions.

Think about driving a car. As the driver, you don’t need to know how the brakes or ignition work to be able to get from point A to point B. If you need to stop, all you know if that pressing on the brake pedal will allow you to slow down and come to a stop. Likewise, if you need to start the car, the only thing you need to know how to do is stick the key in the ignition and turn.

Now, if those things have some sort of problem and don’t work, you will take your vehicle to a mechanic who has that abstract knowledge that can get the vehicle working again. This is how those abstract classes and functions work.

And what makes them work? Well, in part, it’s the job of encapsulation to ensure abstraction works the way it should.

3—Inheritance

Inheritance is achieved through designing your program to have parent classes and child classes. The child classes inherit the logic from the parent class; however, they cannot inherit the logic from another child class underneath the parent. The easiest way to observe how this works is through looking at variable inheritance:

int number = 9;
if (number < 80)
{
string message = “number is less than 80”;
Console.WriteLine(message);
}

Here is what inheritance says about the variables we defined in this code snippet:

  1. number is global, so it can be used anywhere in the program
  2. message, on the other hand, is local, so it can only be used within the code snippet for the if statement

If we tried to print message after the if statement closed, we would get an error because it only survives within the braces it was defined within. Inheritance works the same way with classes, objects, and functions.

4—Polymorphism

This OOP concept refers to the ability of a variable, function, or object to have more than one form. This is done primarily using overrides, which tell your program that the function in the current class or object should be executed over the one in the base class. Let’s look at an example in C# . . . 


class Animal
{
public virtual void sound()
{
Console.WriteLine(“This animal doesn’t make a sound because it’s generic.”);
}
}
class Cat : Animal
{
public override void sound()
{
Console.WriteLine(“Meow.”);
}
}
class Dog : Animal
{
public override void sound()
{
Console.WriteLine(“Woof.”);
}
}

So, here is how the logic works:

  1. The base class isn’t used directly. It’s created to set the structure for the following classes that will specific the right animal in each scenario.
  2. We set up the generic function in the base class that will be overwritten when the specific Animal objects are created.
  3. When we call Cat.sound(), the sound() overrides the function in the base class by customizing the code that gets executed. If we had not used the override keyword, the code would have executed the function in the base class.
  4. Likewise, Dog.sound() would execute in the same way.

And that’s how polymorphism works at the foundational level. There are obviously more complex uses for it, but this simple example works well to introduce you to it.

Popular OOP Languages

  • Java
  • C#
  • Ruby
  • Python
  • PHP
  • Rust

Useful Resources to Learn More about Object-Oriented Programming

Access Modifiers (Microsoft)

OOP Simplified

Polymorphism with C#

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *