What is object-oriented programming (OOP)?

What is object-oriented programming (OOP)?

It took me a while to understand what object-oriented programming means. And to be honest, I'm still learning about it as I progress in my career. But isn’t that what being a developer is all about? I love learning something new and turning it into a product people want to use. That goes hand in hand with continuous development and improvement of one's skills and knowledge.

Back to OOP. If you’re anything like me and have come from a background of minimal coding you may have found it difficult to grasp the meaning behind object-orientated programming. So in this article, we will simplify it in a way that helped me understand.

For more details on the concepts of object-oriented programming Design Patterns by Erich Gamma is a useful resource. SOLID principles and OOP go hand in hand, and this is 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 4x4, or maybe it’s a cover table 4x4?

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 state via private methods. If your code needs to interact with these objects then it 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 or not you can use access modifiers. These modifiers are Private, Protected, and Public with private being the most restrictive and public being the 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 whereas 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 the 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 inherit from house pets and animals in addition to adding their specific implementation.

By following this process we share a codebase between classes. This 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, 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 you use it.

What 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 help.

I 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 go hand in hand with OOP.

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

Did you find this article valuable?

Support Michael Asaad by becoming a sponsor. Any amount is appreciated!