The very basis of Object Programming relies on the concepts of Objects.
So what are objects? and how do they the working of OOP’s.
Before we start understanding object’s under OOP’s lets us first try and understand objects from real world. Let us consider a table lamp, which is lying on our desk. The lamp has certain utility and functions that it performs. The functions of the object and its characteristics define the a lamp. For example the lamp can be either put on or off and the lamp gives light or it can be off so it does not give light. These behaviour of the object can be classified into behaviour and state. The state of a lamp can either be on or off and the behaviour can be turning on or turning off.
Similarly all the other objects that we see in real world have these to parameters to define and distinguish them from each other.
In programming concepts objects exhibit the same parameters and are used by other objects to achieve some task. So that means each programming object has a behaviour and state.
State of objects in programming are called fields or variables and behaviour are called methods or functions. So we use variables to store characteristics of a object and the functions to perform so action on those characteristics.
Let’s understand this with the help of an example.
Say for example we are supposed to build an application to capture employee information and process his wages based on the number of days that he has worked.
So we will create an employee class which will have state or variables like
- name
- age
- no of days worked
- daily wage rate
- payable wages, etc.
Now using this information that we have provided we will write a behaviour or a function which will calculate his wage rate.
This function will basically multiply no of days worked with daily wage rate and return the computed figure as payable wages.
So using the state of the object we perform certain actions on the same to achieve our task.
But why do we need to use objects in our programs?
- Modularity: An object can be written independent of other objects and can be used by other objects. So we can have one object called as employee and another object called as job. Job object can reference the employee object to perform certain tasks. . Once created, an object can be easily passed around inside the system.
- Information – Hiding: Using methods we can hide the structure and details of the object. That means an calling object just needs to know the function name and the values to be passed. What happens inside the object has got no business with calling object. On successful completion the object can return some value to the calling object.
- Code Re-use: Once an object has been created it can be reused in the same project or other projects needing the same functionality. Saves time and resources.
- Debugging and Pluggability: If there is an object that is creating problem in your program, just remove it and replace with another object. It is easier to debug modular structures as we will be testing and checking unit by unit instead of checking the whole code. It helps to narrow to the problem source and faster ramifications.
I will try and cover more on the uses of objects in my coming few articles, where you will learn the real power and use of objects in real life programming.
Some of this knowledge that I have presented comes from sun.com
Will follow up with more relevant articles.
Comments