How to Understand Object Oriented Programming
Understand that there are generally three main parts to object oriented programming., 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...
Step-by-Step Guide
-
Step 1: Understand that there are generally three main parts to object oriented programming.
These are Abstraction, Inheritance and Encapsulation. -
Step 2: In Object-Oriented Programming Languages
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. , 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. , 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. , In Object-Oriented Programming, the two big languages are Java and C++.
Both are good languages for various things.
Many people will try to 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. -
Step 3: programs are designed with the concept of objects
-
Step 4: where each object contains its own set of variables to store data about or relevant to that object
-
Step 5: and functions to perform action (like outputting text or making an API call to the operating system)
-
Step 6: perform calculations
-
Step 7: add data to variables inside the object
-
Step 8: Objects represent various things in a program
-
Step 9: like a Square.
-
Step 10: In the case a math program was intended to calculate the area of not only squares
-
Step 11: but other shapes
-
Step 12: such as a circle
-
Step 13: "Inheritance" would be used.
-
Step 14: Many programmers have a hard time deciding which programming language to use.
Detailed Guide
These are Abstraction, Inheritance and Encapsulation.
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. , 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. , 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. , In Object-Oriented Programming, the two big languages are Java and C++.
Both are good languages for various things.
Many people will try to 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.
About the Author
Kenneth Wood
Creates helpful guides on crafts to inspire and educate readers.
Rate This Guide
How helpful was this guide? Click to rate: