pointing at a computer

What is object-oriented programming (OOP)?

It took me a while to get my head around what object-oriented programming meant. And to be honest I feel like I’m learning more and more about it and its usefulness as I progress in my career. But isn’t that what being a developer is all about? Is that why we are on this career path?

I don’t know about you but I absolutely love the thrill of being able to learn something new all by yourself, then turn it into an actual product that people want to use. That idea completely excites me. And that goes hand in hand with the idea of continuous development and improvement of one’s skills and knowledge. 

“Knowledge is power”

Back to OOP. If you’re anything like me and have come from a background of little or no knowledge of coding you may have found it difficult to grasp the meaning behind object orientated programming. So in this article, I’m going to try and simplify it in terms that helped me understand it.

For more details on the concepts of object-oriented programming Design Patterns by Erich Gamma is a useful resource. SOLID principles and OOP both go hand in hand, and this something we will also cover on the blog.

A practical example of object-oriented programming

Let’s take a car. When you think about a car, what are the first things that pop into your head? Possibly the make, the colour, the engine size, whether it’s a cover table or a 4×4, or maybe it’s a cover table 4×4? 

What do all these things have in common? They are things describing the car, they are properties of the car. When designing your car, you customise it and add as many properties as you like. Features like Bluetooth, parking sensors and xenon lights. All of these are properties of the car object.

Ok. So now that you have your model car, what’s the first thing you’d want to do? After taking the obvious picture or two, you’d want to take it for a spin, maybe to your friends or to the beach to show off? But, to be able to drive the car there has to be some functionality on the car itself to allow you to race it around the block. This functionality is called a method. It is an action that you can instruct the car to carry out on command or based on specific triggers. 

So that’s the basis of what we are going to talk about today. You have your object, the car, it has properties e.g. colour and methods e.g. drive. But why is it useful to write code in this way? To understand this a bit further we will need to look at the four pillars of OOP: Encapsulation, Abstraction, Inheritance, and Polymorphism.

Pillars of Object-oriented Programming

What is Encapsulation?

The first and probably the most important concept of object-oriented programming is encapsulation. This is the process of keeping objects and their state grouped and hidden from the outside code. In C# programming this can be done by creating classes. Methods related to the data are also made part of the class and only public methods can be called outside of the class.

The idea of classes and objects is one of the fundamental concepts of object-orientated programming. The reasoning behind it is to only expose information that needs to be exposed and nothing more. Doing this allows the programmer to hide specific information and control access to the internal state of the object.

The benefit of this is that it allows objects to manage their own state via private methods. If your code needs to interact with these objects then they can only do so via the public methods provided. Programmers can also validate values before updating attributes.

To set whether methods can be accessed outside the object the or not you can use access modifiers. These modifiers are: Private, Protected, Public with private being most restrictive and public being least restrictive.

What is Abstraction?

Following on from encapsulation we have abstraction, the second pillar of object-orientated programming. This is the idea of objects only exposing high-level mechanisms for being used. It means that internal implementation details are hidden from public view.

The benefits of this are as follows:

  • If you, as the programmer, change how the internal programming and implementation of a class works, the high-level mechanisms for using the class won’t change
  • Internal changes are made without affecting the wider code base.
  • High-level mechanisms exposed as “public” are designed such that they can be instinctively used and are rarely changed.

What is Inheritance?

The third pillar of object-orientated programming is inheritance. Inheritance puts into practice the coding principle of DRY, Don’t Repeat Yourself.

Inheritance allows the programmer to reuse common coding logic across several classes. The more generic code is shared from parent classes down to the more specific child classes. Child classes dictate which parent classes they inherit from, creating a hierarchy. For example:

childClass : parentClass

A good example of this is when looking at animals. For example you have a dog, cat and cow. All of these are animals. Dogs and cats are house pets where as a cow is a farm animal. So if we look at a diagram to create a hierarchy you have the following:

Diagram of inheriting from animal as the parent class then house pets and farm animals as child classes.

We place code that is common between animals in general in the animal class. Housepets will inherit from animals and then build upon them with more specific properties and methods. Dogs and cats will then both inherit from house pets and animals in addition to adding their own specific implementation.

By following this process we share a codebase between classes. By doing this it means the programmer does not repeat code, it is easier to debug and make changes to code.

What is Polymorphism?

Polymorphism is the final pillar of object-oriented programming. Put simply, polymorphism is the concept where an interface (you can think of this as a minimum requirement blueprint for the class) defines how we can use a class.

Interfaces allow the programmer to create objects of different types but have the same access methods or properties set.

What is the difference between an interface and an inherited class?

Interfaces do not dictate how a method or property should be implemented or set. They merely ensure that specific items of the class are implemented when the object is created.

Inherited classes are slightly different. They dictate how a method should be implemented. Programmers can then overwrite this in the child classes if needed.

Fun fact: All classes extend the Object class as an interface.

Key take-home features of Object-Oriented Programming

Object-oriented programming allows for more testable, scalable and readable and reliable code. It does take a while to get your head around the four pillars (Encapsulation, Abstraction, Inheritance, Polymorphism), however, as with anything, it does get easier the more your use it.

The thing that helped me the most was trying to understand one thing at a time, see what it means, the benefits of it and how implementing it and using Object-oriented design can really help.

Hope you found this article useful. Please do leave any comments below on how you found learning OOP. If you liked this article you might also like to read about SOLID principles which goes hand in hand with OOP.

For more details on the concepts of object-oriented programming Design Patterns by Erich Gamma is a useful resource.

Leave a Reply

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