Chapter 9: A Second Look at Classes and Objects

Slides:



Advertisements
Similar presentations
Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Advertisements

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.
1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy.
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.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
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.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
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.
Utilities (Part 2) Implementing static features 1.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Chapter 9 – Part Tony Gaddis Modified by Elizabeth Adams.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
A note about coverage Say you have an if statement:
Liang, Introduction to Java Programming, Sixth Edition1 Objects and Classes Gang Qian Department of Computer Science University of Central Oklahoma.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 3.
CSE 1201 Object Oriented Programming Using Classes and Objects.
1  lecture slides online
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
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 13: Introduction to Classes.
Java Classes Introduction. Contents Introduction Objects and Classes Using the Methods in a Java Class – References and Aliases Defining a Java Class.
Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #1 Review.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
A Second Look at Classes and Objects - 1 Static Class Members
Topics Static Class Members Overloaded Methods Overloaded Constructors
Introduction to Classes
Chapter 9: A Second Look at Classes and Objects
Chapter 9: A Second Look at Classes and Objects
Chapter 6 Methods: A Deeper Look
MSIS 655 Advanced Business Applications Programming
Assessment – Java Basics: Part 1
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
String Class.
Presentation transcript:

Chapter 9: A Second Look at Classes and Objects Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis Slides modified, added, deleted by Elizabeth Adams

Chapter Topics Chapter 9 discusses the following main topics: We have discussed those checked either today or previously Static Class Members Passing Objects as Arguments to Methods Returning Objects from Methods The toString method Writing an equals Method Methods that Copy Objects

Chapter Topics Chapter 9 discusses the following main topics: Aggregation The this Reference Variable Enumerated Types Garbage Collection Focus on Object-Oriented Design: Class Collaboration

Review of Instance Fields and Methods Each instance of a class has its own copy of instance variables. Example: The Rectangle class defines a length and a width field. Each instance of the Rectangle class can have different values stored in its length and width fields. Instance methods require that an instance of a class be created in order to be used. Instance methods typically interact with instance fields or calculate values based on those fields.

Static Class Members Static fields and static methods do not belong to a single instance of a class. To invoke a static method or use a static field, the class name, rather than the instance name, is used. Example: double val = Math.sqrt(25.0); Static method Class name

Instances of class Math can NOT be created Material on this slide is from the Java api http://java.sun.com/javase/6/docs/api/java/lang/Math.html public final class Math extends Object The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. All methods in class Math are static.

Instances of class Random can be created Material on this slide is from the Java api http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html public class Random extends Object implements Serializable An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.) If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. No methods in class Random is static

Static Fields Class fields are declared using the static keyword between the access specifier and the field type. private static int instanceCount = 0; The field is initialized to 0 only once, regardless of the number of times the class is instantiated. Primitive static fields are initialized to 0 if no initialization is performed. Examples: Countable.java, StaticDemo.java

Static Fields instanceCount field (static) 3 Object1 Object3 Object2

Static Methods Methods can also be declared static by placing the static keyword between the access modifier and the return type of the method. public static double milesToKilometers(double miles) {…} When a class contains a static method, it is not necessary to create an instance of the class in order to use the method. double kilosPerMile = Metric.milesToKilometers(1.0); Examples: Metric.java, MetricDemo.java

Static Methods Static methods are convenient because they may be called at the class level. They are typically used to create utility classes, such as the Math class in the Java Standard Library. Static methods may not communicate with instance fields, only static fields.

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.

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