Object-oriented programming


Object-oriented programming

It’s been awhile since my last post. I have been enjoying my holidays and I hope you all have too. For the first posts of the year I thought I would brush up on my knowledge and see if I can learn something new.

In this post I will talk about Object-oriented programming. If you have done any sort of coding you would have heard of it. The majority of programing languages support it in one way or another.

Object-oriented programming (OOP) is a fundamental style of computer programming. It is based on the concept of objects. Each object is a data structure that contains data. The data is made up of two types of data. The first being in the form of fields (attributes) and the second being code in the form of procedures (methods).

Object-oriented programming is designing and creating programs out of objects that interact with each other. The most common way to achieve this is using a class-based language. If I use the example of Java, you can create classes, objects are instances of the classes and the classes also determine the type of the object you create.

Object-oriented programming languages include Python, C++, Objective-C, Smalltalk, Delphi, Java, Swift, C#, Perl, Ruby, Lisp and PHP.

If you read a book on the subject they talk about how Object-oriented programming is built on three sturdy pillars: encapsulation, specialization, and polymorphism. If you understand these three pillars or concepts you will be able to effectively do object-orientated programming.


When you create an object it should be fully encapsulated. What I mean by this is that the objects attributes and code inside the objects methods should be hidden from external code. This is useful because it prevents the external code from being concerned with the internal workings of an object. This helps improve code refactoring, for example if the author of an object needs to restructure existing computer code he can do so without worrying about breaking the external code. He just needs to ensure the “public” method calls work the same way.

Advantages include improved code readability and reduced complexity; these can improve source code maintainability and create a more expressive internal architecture or object model to improve future growth


If you have a class and you discover that certain attributes, associations, or methods only apply to some of the objects of the objects it creates, you should use specialization. Specialization is the creation of new subclasses from an existing class. Let’s use a classroom as an example. You create a class called person it has all he attributes and methods that a student object and teacher object need.

This means however when you create a student using the person class you get attributes and methods it doesn’t use and when you create a teacher using the person class you get attributes and methods it doesn’t use. Here we can create a super class called person which has the attributes and methods that both teacher and student use. We then create two subclasses called teacher and student. These subclasses inherit all the attributes and methods of the superclass person.

The advantage here is you have reduced the complexity of the code as 1 class no longer represents 2 objects and you can now edit student object or teacher object without effecting the other. As well as improved the source code maintainability.


Polymorphism is closely tied to inheritance. Polymorphism refers to the ability of a single type or class to take many forms.

For a simple example let’s use shapes. So we have a shape class, we use specialization. Now we have a shape superclass and subclasses circle, square, rectangle etc. Now let’s think of a method all the shape subclasses might do. I am going to pick draw. So all shapes should be able to draw. You could implement and name them all differently in each subclass drawsquare(), rectangledraw() etc.

A more elegant way would be if they all used that same draw() method just named differently. So when you write external code it doesn’t matter which shape is passed it can draw it with the least amount of code. You achieve this with polymorphism. You define an abstract method in the shape superclass call Draw(). Then each subclass is forced to implement the Draw method, but how it does that is up to the class.

Advantage here is you can create a group of objects and the external code doesn’t need to see or worry about how the internal code is written. Again it also improves readability and reduced complexity.


With everything there are Negatives. Object-oriented program typically involved more lines of code, increasing the size of the program. Object-oriented programs are typically slower than procedure based programs. There are certain problems that are inefficient for Object-oriented programs to solve and are better solved by functional-programming style, logic-programming style, or procedure-based programming style.

With hardware size and speed increasing it is easy to understand why even with those negatives the majority of languages support Object-oriented programming.


Leave a Reply

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