DEV Community

Kevin McMinn
Kevin McMinn

Posted on

Basics of Object-Oriented Programming

The first time I learned about Object-oriented programming, I was intrigued by the 'cool' name, but didn't completely understand how it worked. In this blog I discuss a few analogies that helped me better understand OOP.

What is an Object?

In programming, an object is simply a 'thing'. I know, I know...how can you define something as a 'thing'. Well, let's think about it - What do 'things' have? Attributes, right? Let's take a Song for example. A song has attributes! It has a Title, an Artist, a Genre, etc. How about a Dog - A dog has four legs, a color, a name, an owner, and a breed. Though there are millions Dogs with countless names, owners, etc, the one thing that ties them all together are the very fact that every single one can be described as a Dog.

Although this may seem like a not-very informative explanation, these types of examples are what ultimately made me understand Object-oriented programing. The set of activities that an object can perform is an Object's behavior. A dog can bark, wag it's tail, sit, and even shake if it's owner trains them. In the same way, a programmer can create an object and teach it tricks in order to achieve certain goals. In Ruby(my first programming language), EVERYTHING is an object. This means that every piece of code you encounter can perform certain tricks at your command, some are built into Ruby while others can be created at your disposal.

Let's look at a common element in programming, a simple string. As you can see, after the string is defined, I'm able to call different 'methods' or functions on the string I created. Ruby has several built in methods on common objects(ie strings, integers, arrays, and hashes.

An Object's Blueprint

Alt text of image

As previously mentioned, there are MILLIONS of different dogs in the world, but one thing we can all agree on is that they are all Dogs. So how do we create a Dog, Song, Person, etc in our computer programs? First, we need to stitch together a 'blueprint' in order to build our objects. In Ruby, you create a blueprint by defining a 'Class'. You then have the freedom to decide what attributes every object in your class should include. Using our example, if we were to create a 'Dog' class, we would likely decide that every dog needs to have a name, breed, color, and size. The great thing about objects in programming, is that every instance of an object knows everything about itself. We could create a thousand dogs in our program, but still be able to ask each one it's name, breed, etc. This is the magic of Object-oriented programming. Every time you send an object somewhere in your program, you gain access to all of its attributes and can interact with it...just as if a person came to your house, you'd be able to ask anything about them and they can tell you.

In the below example, I've created a Dog class, or template, that each dog instance will be made from. Each and every dog is required to start with a name and breed, and all of the dogs will be able to bark. Woof Woof!

Two Advantages of Object-Oriented Programming

  • Re-Use

    -Consider our Dog example. Let's say our program grows to the point where we need to create other Dog classes. We find out we need to add a Service dogs, which have have several abilities that normal dogs do not have. Service dogs need to know how to lead, comfort, and sense emergency. However, all Service dogs still have a name, breed, and need to be able to bark. This is where inheritance comes in to play, we can easily have our 'Service' dog class inherit everything from our Dog class, and still be able to add certain characteristics of Service dogs.

Alt text of image

  • Encapsulation

    -"Each object forms a separate entity whose internal workings are decoupled from other parts of the system." This means that a part of your program only has access to a specific object and all of its methods/attributes only when explicitly given permission by the programmer. This creates a healthy environment where all objects are protected from each-other, making error identification and debugging easier.

Top comments (4)

Collapse
 
rnrnshn profile image
Olimpio

Great post. I would like to have you talking about polymorphism on OOP.

Collapse
 
kwmcminn profile image
Kevin McMinn

Thanks for the feedback. I'll update with polymorphism as an advantage.

Collapse
 
rnrnshn profile image
Olimpio

Thanks... Can't wait...

Collapse
 
simonfranz profile image
simonfranz

For object orientated programming smalltalk is one of the cleanest way to learn and understand it. A nice and free implementation is #pharo