Class vs Object in Java
In Java, a class defines attributes and methods, while an object is an instance of a class allowing you to call its methods. Learn how to declare attributes and implement methods in a class, create objects, pass objects as arguments, and use services across classes.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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
Class Computer In Class Computer, you can declare all relevant attributes. In Class Computer, you can define/implement all services/methods (non static) Computer class Computer { private string mfr; private int year; public void set_mfr( ) { } public string get_mfr() { } public void editAfile(){ ;save(..);} } - String: mfr - Int: year - Double: price + set_mfr( ){..} + get_mfr(){..} + editAfile() { } Every attribute MUST be private AND you must provide Two methods related to each attribute to allow set a value And get the value !!!
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();
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.
Class USBFlashDrive We better introduce a new class USBFlashDrive and save() is defined as a method in that class Computer class Computer { private string mfr; private int year; - String: mfr - Int: year - Double: price + set_mfr( ){..} + get_mfr(){..} private USBFlashDrive usb; public void set_mfr( ) { } public string get_mfr() { } + editAfile() { } public void set_usb(USBFlashDrive u){ } public USBFlashDrive get_usb(){ } 1 computer association 1 usb usb.save() public void editAfile(){ ; ;} } USBFlashDrive - String: loc + save() { }
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.setusb(myusb); mylaptop.editAfile(); Don t forget to connect myusb to mylaptop!!!
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-8
Passing Objects as Arguments Examples: PassObject.java PassObject2.java A Rectangle object length: 12.0 5.0 width: displayRectangle(box); Address public static void displayRectangle(Rectangle r) { // Display the length and width. System.out.println("Length: " + r.getLength() + " Width: " + r.getWidth()); } 9-9
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.java Method return type: public static BankAccount getAccount() { return new BankAccount(balance); } 9-10
Returning Objects from Methods account = getAccount(); A BankAccount Object balance: 3200.0 address public static BankAccount getAccount() { return new BankAccount(balance); } 9-11
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.java 9-12
Copy Constructors A copy constructor accepts an existing object of the same class and clones it public class Course { private String courseName; private Instructor instructor; private TextBook textBook; 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 = instr; textBook = text; } . } Security Issue!!! public Course(String name, TextBook text Instructor instr, ) { courseName = name; instructor = new Instructor(instr); textBook = new TextBook(text); } . } 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!!! class WhoKnowsFriendOrEnemy { 9-13