Object-oriented programming is the most widely used type of code-writing. It is used in building working pieces of software, e.g. assemblies, and every software package out there utilises it to some extent.
Best well-known programming languages that utilise object-oriented paradigm are C, C++, C#, VB, VBA, Java, Python and 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 elements that are used in object-oriented languages are objects, inheritance, interfaces, packages and polymorphism, which are explained in detail below.
The aim of this article is to guide you through general fundamental principles of object-oriented programming and it is not a substitute for a proper language-specific tutorial. However, we hope that the article would make it easier to understand any in-depth technical learning material.
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 process 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. The best way to describe it is to compare it with real-life objects, such as a car. A car would have a whole bunch of properties, such as colour, 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 others. Events are things that happen to an object that cause object to change its behaviour or properties. In the example with car, an event may be low fuel light lighting up or tyre 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, colour 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 utilise any of its methods or react to any external events.
Sometimes, as a programmer, you would want to use a particular class to perform more tasks than you can with default choice of properties, methods and events. This is when inheritance comes in handy.
Inheritance is when you are writing completely new class but, instead of doing it from the scratch, you are re-using all functionality of an 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 have some additional extras such as illegal spoiler and nitro-injector. In real-life programming this may be used to have some generic form blueprint as a base class and inherit from it to create blueprints for forms used in specific parts of the software.
Interface is something that a class may or may not implement. They look very similar to what classes look like with methods and properties listed, but without actual functional implementation of any of the 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 initialised), 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. 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. There may be a program written to test such component by running specific methods and using specific properties of new classes and the only way to make this program work is to make sure that all of these properties are in there. Likewise, having a program running methods on interfaces rather than specific classes enables classes to be completely interchangeable, as long as they match the definition written in the interface.
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 are working on completely different part of the system and 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 program 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.
Methods of the classes often take various variables as parameters. This is true for the classes themselves, as they have access points known as constructors used for their initialisation, 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. Polymorphism allows to execute the same method (or instantiate the same class) with different sets of parameters.
This is achieved by having multiple methods with the same name or multiple constructors defined in the code. As long as each one of them has unique sets of parameters, it would be valid. We will go back to the car example once again. We may write a constructor for it inside of which a colour gets set to a default at the point of instantiation. We may also write a second constructor which takes a free-text colour name and set the colour 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 colour, or setting the colour to the one of our liking by passing it as the parameter.
If you want to learn a particular programming language in-depth, here is a list of great tutorials, all which can be done free of charge:
Written by Mobile Tech Tracker team
Posted on 29 Aug 2015