Docsity

Object Oriented Methodology - OOM - Part 4, Study notes of Object Oriented Programming

Toggle sidebar
bg1
1
Object Oriented Methodology
Object Oriented Methodology
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c

Partial preview of the text

Download Object Oriented Methodology - OOM - Part 4 and more Study notes Object Oriented Programming in PDF only on Docsity!

1

Object Oriented Methodology Object Oriented Methodology

2

OOM OOM

 AboutAbout OOP :OOP :

 (^) Procedure Oriented Vs. Object Oriented

 OOP ConceptsOOP Concepts

 (^) Objects & Classes  (^) Elements of the Object Model  (^) Problem solving in OOP methodology

 Implementation issuesImplementation issues

 (^) OOP implementations: Java Vs. C++ etc.  (^) Working of a Java Program

  • (^) Compiler-Interpreter & Java Programming  (^) Java Functionality: Servlets & Applets?
  • (^) Platform independent : JVM
  • (^) Console Operation Vs. IDE : JDK Vs. NetBeans  (^) Tools needed: JDK , IDE  (^) Writing Java Class  (^) Adding Methods to a Class

 Object Oriented ModelingObject Oriented Modeling

C++, Java & C # C++, Java & C

Similarities with C++  (^) – Userdefined classes can be used like builtin types.  (^) Basic syntax

Differences from C++  (^) – Methods (member functions) are the only function type  (^) – Object is the topmost ancestor for all classes  (^) – All methods use the runtime, not compiletime, types (i.e. all Java methods are like C++ virtual functions)  (^) – The types of all objects are known at runtime  (^) – All objects are allocated on the heap (always safe to return objects from methods) Single inheritance only Comparisons to C#  (^) – C# very similar to Java in OOP. For details, see  (^) http://www.harding.edu/fmccown/java1_5_csharp_comparison.html

4

Object Oriented Methodology Object Oriented Methodology

Objects & Classes in Java Objects & Classes in Java

 (^) Writing a Class in JavaWriting a Class in Java

 Adding Fields, Adding Methods to a Class

 Instantiating an Object : Understanding the references

 Issues related to memory : Garbage collection

 Using Dot Operator

 The this reference

 Method signature

 Method Overloading

 (^) OOM design issuesOOM design issues

 OOM Characteristics: When to use / not to use?

 Modeling in OOM way …Modeling in OOM way …

5

Simple Class Example:OOM design Simple Class Example:OOM design

 (^) 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.

 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.

OOPS…. OOPS….

 (^) For example, The (^) isSalaryisSalary field could also be used within the computePay() method to determine which arithmetic to use because computing pay for salaried employees is different than for hourly employees. public float computePay() {{ if(isSalary) { //Perform arithmetic for salaried employee } else { //Perform arithmetic for hourly employee } }}

8

OOM Design OOM Design

 A bigger problem arises when something needs to be

changed. What happens to the isSalary field when a new

type of employee needs to be added?

 Suppose that the company starts hiring contractors who are

paid by the day. The Boolean no longer works because it

can’t be used to distinguish between three types.

 You could change the field to an int, name it employeeType,

and use an enumeration like 0 for salary, 1 for hourly, and 2

for contractor.

 Again, you could make this work, but you have to make serious

modifications to the Employee class. The computePay() method

will have to entirely rewritten:

10

Revised computePay method Revised computePay method

public float computePay() { switch(employeeType) { case 0: //Perform arithmetic for salaried employee break; case 1: //Perform arithmetic for hourly employee break; case 2: //Perform arithmetic for contractor employee break; //and so on if necessary } }

11

Two Classes Two Classes

public class Salary { public String name; public String address; public int SSN; public int number; public float salary; public void mailCheck() { System.out.println(“Mailing a check to “

  • name
  • “ “ + address); } public float computePay() { return (float) salary/52.0; } }

public class Hourly { public String name; public String address; public int SSN; public int number; public float hourlyRate; public float hoursWorked public void mailCheck() { System.out.println(“Mailing a check to “ + name + “ “ + address); } public float computePay() { return (float) hoursWorked * hourlyRate; } }

13

Example: OOM design Example: OOM design

 (^) As you can see, the Salary and Hourly classes are repeating code. Salary and hourly employees are still employees, and there is a lot of information in common between the two.

 (^) Although Salary and Hourly employees are different types, they are not entirely different. Which is seen by the repetition of fields and methods in these two classes.

 (^) The Salary and Hourly classes demonstrate the need for inheritance.

 (^) Using inheritance will improve this design considerably, not only to avoid repeating code, but also to create a program design that allows for better maintenance and code changes later.

 Therefore, writing two separate classes for the two different types of

employees is not yet a satisfactory solution.

27/11/14 01:09 PM Marc Conrad - University of Luton 16

Inheritance Inheritance

Inheritance is a way of relating two classes so that one class may use another class's members without redefining them. Inheritance is what separates abstract data type (ADT) programming from OO programming.

 (^) The basic principle is simple:  (^) A class gets the state and behaviour of another

class and adds additional state and behaviour.

 (^) AbstractionAbstraction is the ability to write an abstract class. An abstract class is a class that cannot be instantiated.

27/11/14 01:09 PM Marc Conrad - University of Luton 17

What is the purpose of Inheritance? What is the purpose of Inheritance?

 (^) Specialisation: Extending the functionality of an existing class.  (^) Generalisation: Sharing commonality between two or more classes.  (^) Polymorphism. Implementation of different behaviour to the same message (depending on the type of the object).

27/11/14 01:09 PM Marc Conrad - University of Luton 19

Inheritance vs. Aggregation Inheritance vs. Aggregation

 (^) Inheritance means that one class inherits the characteristics of another class. This is also called a “is a” relationship:

 (^) Aggregation describes a “has a” relationship. One object is a part of another object.

A car is a vehicle

A car has wheels

A student is a person

A person has legs

Inheritance and “is a” Inheritance and “is a”

We can understand this the “is a” idea

A subclass object “is a” superclass object However, some extra instance variables and methods may have been added and some other methods may have been changed

Note that “is a” is a one way operation

Subclass “is a” superclass (specific "is a" general)

  • (^) With modifications / additions Superclass is NOT a subclass (general not "is a" specific
  • (^) Missing some properties