25-08-2017, 09:32 PM
Procedure Oriented Vs Object Oriented
Procedure Oriented.ppt (Size: 1.1 MB / Downloads: 20)
Example Problem
Suppose that you want to write a program to weekly pay the employees of a company.
Paying employees involves computing their pay based on hours worked or a portion of an annual salary.
In addition, you have to compute Social Security and Medicare taxes, as well as any federal income taxes to be withheld.
Each of these computations has to be repeated for each employee in the company.
Because these tasks are repeated, we can write a procedure for each one.
For example, we might write a procedure called computePay() that inputs an employee’s payment data and returns his or her pay.
You may also have procedures called computeMedicareTax(), computeSSTax(), and so forth.
Programming in OO manner
Let’s take another look at the example in which a program is to be written to pay employees of a company on a weekly basis.
Instead of approaching this problem from Procedure Oriented (the point of view of all the little tasks that need to be performed), such as computing an employee’s pay and taxes, you begin by determining the objects in the problem domain.
An object is any person, thing, or entity that appears in the problem domain.
We can think of procedural programming as writing a procedure for the verbs in the problem domain, such as paying an employee or computing taxes.
You can think of object-oriented programming as writing a class for each of the nouns in the problem domain.
Object Instantiation & References
We could declare the reference e and instantiate the Employee object in a single statement:
Employee e = new Employee();
This statement creates two separate elements in memory: the reference e and the Employee object.
The reference e is not an object.
The object itself does not have a variable name, and the only way you can access and use the object is to use a reference to the object.
Garbage Collection
In Java, there is no keyword or operator that you can use to remove an object from memory. Java was designed to avoid the problems of memory leaks that arise in other languages.
A JVM has a low-level thread known as the garbage collector that is constantly running in the background, looking for objects in your Java program that are no longer being used and freeing their memory
Simple Class Example: OOM design
Let us continue with the simple example of Preparing software for Employee Pay roll calculation.
The design of the Employee class might seem fine initially.
The Employee class has a field named salary, which implies that an employee has a salary; however, hourly employees have an hourly rate, which is quite different from a salary.
So we need computePay for both the Annual salary and hourly wages employee.
Instant idea: A tempting fix for this situation is to add a Boolean field to the Employee class named isSalary that is true for salary employees and false for hourly employees.
The salary field could be used to represent an annual salary when isSalary is true or an hourly rate when isSalary is false.
Methods Are Associated with an Object
A common mistake that is often seen that students try to invoke a method without using a reference, because this is how methods are invoked in procedural languages.
Consider the following statements, the second of which does not compile:
Employee e1 = new Employee(), e2 = new Employee(); //Valid
mailCheck(); //Error. Mail check to whom?
To demonstrate my point, I instantiated two Employee objects and then attempted to invoke mailCheck() without a reference. First of all, this can never be done in Java.
A method can be invoked only by using a reference unless the method is static, in which case a class name is used with the dot operator.
Second, because there are two Employee objects, there are two mailCheck() methods.
Which one do I want to invoke? I need to specify this using the appropriate reference and the dot operator.