Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class and Object. Class vs Object Let us consider the following scenario… Class defines a set of attributes/fields and a set of methods/services. Object.

Similar presentations


Presentation on theme: "Class and Object. Class vs Object Let us consider the following scenario… Class defines a set of attributes/fields and a set of methods/services. Object."— Presentation transcript:

1 Class and Object

2 Class vs Object Let us consider the following scenario… Class defines a set of attributes/fields and a set of methods/services. Object is an instance of a class. Object allows you to call the methods defined in the Class

3 Class Computer In Class Computer, you can declare all relevant attributes. In Class Computer, you can define/implement all services/methods (non static) Computer - String: mfr -Int: year -Double: price Every attribute MUST be private AND you must provide Two methods related to each attribute to allow set a value And get the value !!! + set_mfr(…){..} + get_mfr(){..} + editAfile() {…} class Computer { private string mfr; private int year; … public void set_mfr(…) {…} public string get_mfr() {…} public void editAfile(){… ;save(..);} }

4 Object Computer You cannot use any services provided by a computer until you have a computer. Computer myPC = new Computer(“IBM”, 2012,1000); myPC.editAfile(); Computer mylaptop = new Computer(“Dell”, 2010,900); mylaptop.editAfile();

5 USB Flash Drive Assume USB Flash correctly implements the save data service via method save() Where should you put method save() 1.Copy save() method to Class Computer 2.Create a separate class for USB Flash Drive where save() is provided.

6 Class USBFlashDrive We better introduce a new class USBFlashDrive and save() is defined as a method in that class Computer - String: mfr -Int: year -Double: price + set_mfr(…){..} + get_mfr(){..} + editAfile() {…} USBFlashDrive - String: loc + save() {…} class Computer { private string mfr; private int year; … public void set_mfr(…) {…} public string get_mfr() {…} public void editAfile(){… ; ;} } private USBFlashDrive usb; usb.save() public void set_usb(USBFlashDrive u){…} public USBFlashDrive get_usb(){…} association 1 usb 1 computer

7 Now You can use the save() service You need to buy a computer and a usb flash drive Computer mylaptop = new Computer(“Dell”, 2010,900); USBFlashDrive myusb = new USBFlashDrive(…); mylaptop.editAfile(); Don’t forget to connect myusb to mylaptop!!! mylaptop.setusb(myusb);

8 9-8 Passing Objects as Arguments Objects can be passed to methods as arguments. Java passes all arguments by value. When an object is passed as an argument, the value of the reference variable is passed. The value of the reference variable is an address or reference to the object in memory. A copy of the object is not passed, just a pointer to the object. When a method receives a reference variable as an argument, it is possible for the method to modify the contents of the object referenced by the variable.

9 9-9 Passing Objects as Arguments Examples: PassObject.java PassObject2.java PassObject.java PassObject2.java displayRectangle(box); public static void displayRectangle(Rectangle r) { // Display the length and width. System.out.println("Length: " + r.getLength() + " Width: " + r.getWidth()); } A Rectangle object length: width: 12.0 5.0 Address

10 9-10 Returning Objects From Methods Methods are not limited to returning the primitive data types. Methods can return references to objects as well. Just as with passing arguments, a copy of the object is not returned, only its address. See example: ReturnObject.javaReturnObject.java Method return type: public static BankAccount getAccount() { … return new BankAccount(balance); }

11 9-11 Returning Objects from Methods account = getAccount(); public static BankAccount getAccount() { … return new BankAccount(balance); } balance: 3200.0 address A BankAccount Object

12 9-12 Methods That Copy Objects There are two ways to copy an object. – You cannot use the assignment operator to copy reference types – Reference only copy This is simply copying the address of an object into another reference variable. – Deep copy (correct) This involves creating a new instance of the class and copying the values from one object into the new object. – Example: ObjectCopy.javaObjectCopy.java

13 9-13 Copy Constructors A copy constructor accepts an existing object of the same class and clones it public class Course { private String courseName; // Name of the course private Instructor instructor; // The instructor private TextBook textBook; // The textbook public Course(String name, TextBook text Instructor instr, ) { courseName = name; instructor = new Instructor(instr); textBook = new TextBook(text); }…. } public class Course { private String courseName; private Instructor instructor; private TextBook textBook; public Course(String name, Instructor instr, TextBook text) { courseName = name; instructor = instr; textBook = text; }…. } class WhoKnowsFriendOrEnemy { public static void main(String[] args) { TextBook t = new TextBook(…); Instructor ins= new Instructor(…); Course cs1120 = new Course(“John”,t,ins); // what happens to t and ins; … } } Not Affected Affected!!! Security Issue!!!


Download ppt "Class and Object. Class vs Object Let us consider the following scenario… Class defines a set of attributes/fields and a set of methods/services. Object."

Similar presentations


Ads by Google