Understanding object-oriented programming can be a difficult task no matter what language you are using. However, understanding the underlying concept can make programmatic understanding and usage much easier.
[notice type="support" title="STEP BY STEP" tag="h4"]STEP[/notice]
Understand that there are generally three main parts to object oriented programming. These are Abstraction, Inheritance and Encapsulation.
In Object-Oriented Programming Languages, programs are designed with the concept of objects, where each object contains its own set of variables to store data about or relevant to that object, and functions to perform action (like outputting text or making an API call to the operating system), perform calculations, add data to variables inside the object, etc. This contrasts with Structured Programming Languages, in which programs are designed with collections of functions that are called in different parts of the program, more like a script or job list.
Objects represent various things in a program, like a Square. This is called Abstraction. An object for a square in a math program would have a set of variables, length and width, that can only be accessed by functions inside the object. This is called Encapsulation. This concept reduces the chance of ‘breaking’ something when programming, as when done properly only the object itself can directly manipulate its variables, so debugging problems is easier. The next part of an object, functions, represent things that the object can do or ways to interact with it. In the case of the square object, a function called “area” would calculate the area of the square the object represents, using the contents of the length and width variables inside the object. Those variables may already contain the length and width of the square, in the case they did not, the area function might also take user input for the length and width of the square the object represents and then calculate the area based on the user input.
In the case a math program was intended to calculate the area of not only squares, but other shapes, such as a circle, “Inheritance” would be used. Inheritance is when an object is created based on another object, so therefore, it “inherits” all of its variables and functions. For example, an extension to the earlier square area calculation program would have a main object, called a polygon. In geometry, a polygon is basically a “closed shape”, i.e a square, triangle, circle, cube, etc. All polygons can have their area calculated, just the method of achieving such is different. So, in our program, the polygon object would have length and width variables, and an area function. Since a polygon is not actually a shape, but a class of shapes, the area function would not do anything. When you want to create an object for a particular kind of polygon (i.e a triangle), you would use inheritance to create the object with all the properties of its “parent”, the polygon object. You would then cite the actions the area function inherited would perform. This concept is called “Inheritance”. It makes much more sense when dealing with large programs, where objects typically contain lots of lines of code, in which rewriting an object similar to another would be tedious and pointless, when Inheritance can be used to get the same properties and methods of the parent object.
Many programmers have a hard time deciding which programming language to use. In Object-Oriented Programming, the two big languages are Java and C++. Both are good languages for various things. Many people will try and tell you that one is good and the other is bad, though you should try both and decide for yourself which one you like more.
[notice type="information" title="TIPS" tag="h4"]
- Depending on the programming language, functions the programmer does not want to be inherited by “child” objects can be manually exempted using a specific keyword.
- Don’t get caught up in the language wars, decide which language you like to program in the most, as programming in a language you find boring or incomplete can be tedious and just a job, and not fun.
[notice type="warning" title="WARNINGS" tag="h4"]
- This how-to is only a start towards Object Oriented Programming. To fully understand this programming style, you need to use a programming language that fully supports object-oriented programming – Ruby, Java, Python, C#, VB.NET and Delphi are good choices.
- Learning this new programming style will take some time, and it may take a while before you fully “get it.”
[notice type="support" title="VIDEO" tag="h4"]VIDEO[/notice]
[video_skin style="1" title="Watch this video..." tag="h2" player="youtube" video_id="CGd3S4EjXXA" controls="yes" width="485" height="295"]