Object-oriented programming explained

Object-oriented programming is the most widely used type of code-writing. It is used in building working pieces of software, such as assemblies, and every software package out there utilizes it to some extent.

Some of test well-known programming languages that utilize object-oriented paradigm are C, C++, C#, VB, VBA, Java and Python, while there are also many others.

The most popular syntax style used in object-oriented programming is derived from C language. Therefore it is not always easy to instantly say whether something is written in Java or C#. Likewise, if you know either of these languages, it would not be difficult to learn another.

The core concepts that are used in object-oriented languages are objects, inheritance, interfaces, packages and polymorphism, which are explained in more detail below.

The aim of this article is to guide you through general fundamental principles of object-oriented programming. It is not a substitute for a proper language-specific tutorial. However, it would provide you a good introduction to the subject.

Objects

As you may have guessed from the name, objects form the most fundamental part of any object-oriented programming language. There are normally two definitions for word “object”: the most fundamental data type and instantiated class. In the first situation, object is a declared variable that can be given a value of any data type (e.g. decimal, integer, free text etc.). However the second definition is of more concern to us in the context of learning object-oriented programming.

Code is written in modules, the most widely used of which is called class. Class is a blueprint for an object, which only becomes useful when it is initiated, which is programmer’s way of saying “becomes available to use by reserving space for it in the memory”. Classes have properties, methods and events, all of which can be used after the class is instantiated as an object.

The best way to describe an object is to compare it to a real-life object, such as a car. A car would have a whole bunch of properties, such as color, top speed, number of gears, number of doors, make etc.. What programmers refer to as “methods” are the actions that an object is capable of performing, therefore in our car example, methods would be “increase speed”, “decrease speed”, “change gear”, “toggle headlights” and many other possible actions.

Events are things that happen to an object that cause object to change its behavior or properties. In the example with a car, an event may be a low fuel light lighting up or a tire being punctured. In software development, examples of common events are “on click” associated with buttons or “on load” associated with web pages.

In our car example, a class would be just an abstraction of an actual car, so it may not have defined make, color or top speed. However, as soon as this class is instantiated, all of these properties become defined and the object turns into actual functioning car. The car is now ready to utilize any of its methods or react to any external events.

Inheritance

Sometimes, as a programmer, you would want to use a particular class to perform more tasks than you can with the default choice of properties, methods and events. This is when inheritance comes in handy.

Inheritance is when you are writing a completely new class but, instead of doing it from scratch, you are re-using all functionality of the existing class. Depending on the programming language in use, the original class would be referred to as either base class or super-class.

In our car example, we may have a sub-class “modified car” that inherits from the original class “car”. Our new class will have everything that the original class has, but may also have some additional extras, such as illegal spoiler and nitro-injector. In real-life programming, inheritance may be used where you have some generic blueprint for a user form as a base class and then inherit from it to create blueprints for various user forms to be used in specific parts of the software.

Interfaces

Interface is something that a class may implement. They look very similar to what classes look like, with methods and properties listed, but they don’t have the actual functional implementation in either classes of properties.

If a class implements an interface, it has to have all the components that the interface lists; however, it is up to the class to implement any of these components in its own way.

Going back to the car example, there may be an interface which says that any car should have a number of doors, top speed and a number of wheels as properties. There may be cars with 6 wheels or 4 wheels (which, in this case, would be defined either in the class itself or after the class has been initialized), but if you have something that doesn’t have a number of wheels defined, it is certainly not a valid car.

It is not always easy to grasp real-life significance on the use of interfaces, especially if you are novice programmer. However, interfaces are really important.

Possibly, the best examples would be unit-testing and writing interchangeable modules. In both cases, interfaces are there to make sure that any new class would work with the existing system.

Unit-testing is when small newly-written components of the software are tested individually, outside of the greater system. In this case, any concrete class that you write unit tests for can take interfaces as parameters, so absolutely any concrete class that implements an interface can be passed as a parameter. When this happens, you can pass simplest possible implementations of these interfaces into the class that is being tested, so the scope of your testing will be limited to a single class and no other components of the system will be covered.

Packages

Packages and namespaces are the way to group various classes into their logical categories, making the code well-organised and easy to follow. They also prevent naming conflict in a program. For example, two teams of developers who are working on completely different parts of the system may have unintentionally written two completely unrelated classes with the same name. In this situation, if you will then tell the program to instantiate a class with this particular name, you’ll get an error, as the compiler will not know which of the classes to instantiate. Luckily, as these classes have been written for completely different parts of the system, chances are that they have been written for different packages. In this situation, the program can be told to instantiate a particular class by using fully qualified name, i.e. package name followed by the class name.

With our car example, we may have a package called “motor transport” and the one called “rail transport”, both of which may have a class called “car”. In this situation, there will be no confusion over which kind of car to pick.

Polymorphism

Methods of the classes often take various variables as parameters. Classes have have access points used in instantiation, known as constructors, which may or may not take parameters. Likewise, they have methods, which also may or may not take parameters.

As a class may be accessed from various parts of the code, there may be situations where particular parameters are not available or suitable substitutes of different data type are available instead. This is where Polymorphism comes in.

Polymorphysm is when you have multiple methods with the same name that return the same data type or multiple constructors with different sets of parameters defined in the code of a class. As long as each one of the constructors or the methods has a unique sets of parameters, it would be valid.

We will go back to the car example once again. We may write a constructor without parameters for it that sets the color to a particular default value. We may also write a second constructor which takes a free-text color name and sets the color property to be whatever is passed as the parameter. So now, we may instantiate our car by either not giving it any parameters at all if we aren’t too bothered about its color, or by passing any color name that we want as an instantiation parameter.

Further resources

If you want to learn a particular programming language in-depth and use the object oriented concepts in practice, here is a list of great tutorials, all which can be done free of charge:

W3 Schools

Tutorials Point

Official Microsoft Visual Basic resources

Official Microsoft C# resources

Official Oracle Java resources

Leave a Reply

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