1 / 28

CA591/598 – Chapter 9 Object-Oriented Programming

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.

tillie
Download Presentation

CA591/598 – Chapter 9 Object-Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CA591/598 – Chapter 9Object-Oriented Programming Dr. James Murphy jamurphy@computing.dcu.ie

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

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

  4. General Format of get and set Methods • Get method: public return_type getVariable() { return variable; } • Set method: public void setVariable(type param) { variable = param; }

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

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

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

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

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

  10. Driver Program for Class Lotto

  11. Referencing Objects

  12. Referencing Objects Example fred harry emp2 emp1 1000 2000 name name Fred Murphy Harry White salary salary 15000 20000

  13. Referencing Objects Example fred harry emp2 emp1 2000 1000 2000 1000 name name Fred Murphy Harry White salary salary 15000 20000

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

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

  16. Example: UseAccounts2 joe mary 1000 3000 accountNo accountNo name name balance balance printAcc(BankAccount acc) acc

  17. Example: UseAccounts2 joe mary 1000 3000 accountNo accountNo name name balance balance printAcc(BankAccount acc) acc 1000

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

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

More Related