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.

Slides:



Advertisements
Similar presentations
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Review of C++ Programming Part II Sheng-Fang Huang.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 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.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Static class members.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 9 – Part Tony Gaddis Modified by Elizabeth Adams.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
PART 1 Part 1: The Material. Whether you knew it or not, all the programming that you have performed until now was in the boundaries of procedural programming.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
 2005 Pearson Education, Inc. All rights reserved Introduction to 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 14: More About Classes.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #1 Review.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
Chapter 9 A Second Look at Classes and Objects - 3 Aggregation.
Chapter 9 A Second Look at Classes and Objects - 2.
A Second Look at Classes and Objects - 1 Static Class Members
Chapter 14: More About Classes.
Programming Logic and Design Seventh Edition
Introduction to Classes and Objects
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Topics Static Class Members Overloaded Methods Overloaded Constructors
Chapter 3: Using Methods, Classes, and Objects
A Second Look at Classes and Objects - 2
Chapter 14: More About Classes.
Chapter 9: A Second Look at Classes and Objects
Chapter 9: A Second Look at Classes and Objects
Introduction to Classes
Chapter 9 Object-Oriented Programming: Inheritance
Defining Classes and Methods
Object Oriented Programming Review
Object Oriented Programming in java
Dr. Sampath Jayarathna Cal Poly Pomona
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
SPL – PS3 C++ Classes.
Presentation transcript:

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 fact, an object of the class does not even have to exist in order for values to be stored in the class’s static fields. You can think of static fields and static methods as belonging to the class instead of an object of the class.

Static Fields When a field is declared with the key word static, there will be only one copy of the field in memory, regardless of the number of objects of the class that might exist. A single copy of a class’s static field is shared by all objects of the class. See example

Static Methods When a class contains a static method, it isn’t necessary for an object of the class to be created in order to execute the method. See example

Passing Objects As Arguments to Methods To pass an object as a method argument, you pass an object reference. See example When writing a method that receives the value of a reference variable as an argument, you must take care not to accidentally modify the contents of the object that is referenced by the variable.

Returning Objects from Methods A method can return a reference to an object. See example

The toString method Most classes can benefit from having a method named toString, which is implicitly called under certain circumstances. Typically, the method returns a string that represents the state of an object. See example

Writing an equals method You cannot determine whether two objects contain the same data by comparing them with the == operator. Instead, the class must have a method such as equals for comparing the contents of objects. See example

Methods that Copy Objects You can simplify the process of duplicating objects by equipping a class with a method that returns a copy of an object. You cannot make a copy of an object with a simple assignment statement: Stock company1 = new Stock(“XYZ”, 9.62); Stock company2 = company1; See example

Copy Constructors Another way to create a copy of an object is to use a copy constructor. A copy constructor is simply a constructor that accepts an object of the same class as an argument. It makes the object that is being created a copy of the object that was passed as an argument. See example

Aggregation Aggregation occurs when an instance of a class is a field in another class. In real life, objects are frequently made of other objects. A house is made of door objects, window objects, wall objects, and much more. It is the combination of all these objects that makes a house object. When designing software, it sometimes make sense to create an object from other objects.

Aggregation Suppose you need an object to represent a course that you are taking in college. You decide to create a Course class, which will hold the following information: 1. The course name 2. The instructor’s last name, first name, and office number 3. The textbook’s title, author, and publisher

Aggregation A good design principle is to separate related items into their own classes. In this example an Instructor class could be created to hold the instructor-related data and a TextBook class could be created to hold the textbook-related data. Instances (objects) of these classes could then be used as fields in the Course class. See example

Course - courseName : String - Instructor : Instructor - textBook : TextBook + Course(name : String, instr : Instructor, text : TextBook) + getName() : String + getInstructor() : Instructor + getTextBook() : TextBook + toString() : String TextBook - title : String - author : String - publisher : String + TextBook(title : String, author : String, publisher : String) + TextBook(object2 : TextBook) + set(title : String, author : String, publisher : String) : void + toString() : String Instructor - lastName : String - firstName : String - officeNumber : String + Instructor(lname : String, fname : String, office : String) +Instructor(object2 : Instructor) +set(lname : String, fname : String, office : String): void + toString() : String Aggregation in UML diagrams