280 likes | 469 Views
CA591/598 – Chapter 9 Object-Oriented Programming. Dr. James Murphy jamurphy@computing.dcu.ie. Classes and Objects. Consolidate concepts from chapter 8. Understand encapsulation. Public and private modifiers. Referencing objects. Passing object reference to method.
E N D
CA591/598 – Chapter 9Object-Oriented Programming Dr. James Murphy jamurphy@computing.dcu.ie
Classes and Objects • Consolidate concepts from chapter 8. • Understand encapsulation. • Public and private modifiers. • Referencing objects. • Passing object reference to method. • Co-operation between objects. • Read and understand programs that use objects.
Encapsulation • Definition: Combining of behaviour and data into program units called objects. • Hide the data behind a specific interface that controls access to the data. • To provide access to data, use “get” or “set” methods. • e.g. getNum1() or setNum1(num) • Key to encapsulation – never access instance variables directly, only through object’s methods.
General Format of get and set Methods • Get method: public return_type getVariable() { return variable; } • Set method: public void setVariable(type param) { variable = param; }
Visibility Modifiers: public/private • Specify access characteristics of members of a class. • What extent they can be accessed or referenced. • private: variable or method has no access outside its own class • public: allows access from outside the class • “Information hiding”: operations from outside the encapsulated unit (class) cannot affect data inside the unit.
Public and private Methods • Object-oriented programming has client-server approach. • Program is composed of different objects co-operating. • Object can provide a service to another object. • The one receiving is the client. • Public methods act as interface. • Private methods concerned with internal workings, i.e. delegating sub-tasks within the class.
Example: Tunes.java • Tunes class contians main method which instantiates one object from class CD_collection. • CD_collection used to keep track of the number of CDs in a collection and their value.
Another Example – Lotto Class printNumbers() (public method) selectNumbers() (private method) Display Numbers (code in printNumbers) Select number Check if not on list Add to list
Class Lotto • Instance variables: • An array to hold six lotto numbers • Public method: • void printNumbers() • Displays 6 numbers on the screen. • Private methods: • void selectNumbers() • Called from printNumbers() • Objective: Chooses 6 unique random numbers. • Calls searchList() to check number not on list already • boolean searchList(int num, int end) • Called from selectNumbers() • Input arguments: no. to be searched for, limit of search • Objective: Searches array for number. Finishes if found or all filled elements searched
Referencing Objects Example fred harry emp2 emp1 1000 2000 name name Fred Murphy Harry White salary salary 15000 20000
Referencing Objects Example fred harry emp2 emp1 2000 1000 2000 1000 name name Fred Murphy Harry White salary salary 15000 20000
Referencing Objects • More than one variable can hold the address of the same object. • e.g. emp1 = fred; • Copies the address of the object from fred to emp1. • Multiple references can refer to same object, called aliases of each other.
Passing Objects to Methods • When passing a variable to a method, its value gets copied into the method. • When reference variable is passed to a method, the same object can then be referenced from inside the method.
Example: UseAccounts2 joe mary 1000 3000 accountNo accountNo name name balance balance printAcc(BankAccount acc) acc
Example: UseAccounts2 joe mary 1000 3000 accountNo accountNo name name balance balance printAcc(BankAccount acc) acc 1000
Co-Operating Objects • An object can have within it a reference to another object. • i.e. has a reference variable as one of its instance variables. • e.g. private BankAccount cust; • Example: RunBank
joe mary 1000 3000 accountNo accountNo 1235 1234 name name Mary White Joe Soap balance balance 500 800 joeTrans maryTrans 4000 5000 cust cust 2000 1000 transAmt transAmt 300 400