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.

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
9-1 Chapter.8 Classes & Objects: A deeper look –Static Class Members –Passing & Returning Objects from Methods –The toString method –Writing an equals.
Chapter 8: A Second Look at Classes and Objects
© 2010 Pearson Addison-Wesley. All rights reserved. 9-1 Chapter Topics Chapter 9 discusses the following main topics: –Static Class Members –Passing Objects.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Starting Out with Java: From Control Structures through Objects 5 th edition By Tony Gaddis Source Code: Chapter 8.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
9-2  Static Class Members  Passing Objects as Arguments to Methods  Returning Objects from Methods  The toString method  Writing an equals Method.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects CS 146 Class Notes: Cooper & Ji & Varol Fall 09.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Chapter 9: A Second Look at Classes and Objects
Chapter 9 – Part Tony Gaddis Modified by Elizabeth Adams.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Programming Languages and Paradigms Activation Records in Java.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
A Second Look At Classes And Objects Chapter 9. Static Class Members When a value is stored in a static field, it is not in an object of the class. In.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Topics Instance variables, set and get methods Encapsulation
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #1 Review.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Chapter 9 A Second Look at Classes and Objects - 3 Aggregation.
Chapter 9 A Second Look at Classes and Objects - 2.
Procedural and Object-Oriented Programming
Computer Organization and Design Pointers, Arrays and Strings in C
Chapter 14: More About Classes.
Topics Static Class Members Overloaded Methods Overloaded Constructors
Chapter 8 Classes and Objects
A Second Look at Classes and Objects - 2
Java Review: Reference Types
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
Chapter 14: More About Classes.
Chapter 9: A Second Look at Classes and Objects
Chapter 9: A Second Look at Classes and Objects
Pass by Reference, const, readonly, struct
Defining Your Own Classes Part 1
Classes & Objects: Examples
Variables and Their scope
Implementing Classes Chapter 3.
Today’s topics UML Diagramming review of terms
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Object Oriented Programming Review
Dr. Sampath Jayarathna Cal Poly Pomona
Presentation transcript:

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 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 - 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(..);} }

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

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);

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 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: Address

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); }

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

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

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