




































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





































































1
2
(^) Procedure Oriented Vs. Object Oriented
(^) Objects & Classes (^) Elements of the Object Model (^) Problem solving in OOP methodology
(^) OOP implementations: Java Vs. C++ etc. (^) Working of a Java Program
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
(^) Writing a Class in JavaWriting a Class in Java
(^) OOM design issuesOOM design issues
5
(^) 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.
(^) 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
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 “
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
(^) 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.
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
(^) 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
(^) 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 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
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)