Chapter 6 A First Look at Classes Contents 1. Classes and objects 2. Instance Fields and Methods 3. Overloading Methods and Constructors 5.
Scope of Instance Fields 6. Packages and import Statements 7. Focus on Object-Oriented Design: Finding the Classes and Their Responsibilities 2 1. Classes and Objects Procedures typically operate on data items that are separate from the procedures.
Procedural programming is centered on creating procedures. Object oriented programming is centered on creating objects. An object is a software entity that contains data and procedures. Data: Fields 3 Procedures: Methods 1.
Classes and Objects An object's fields are called attributes. A class is the blueprint for an object. It specifies the fields and methods a particular type of object has. From the class, one or more objects may be created.
Classes and Objects Object oriented programming addresses the problem of code/data separation through: Encapsulation Data Hiding Encapsulation refers to the combining of data and code into a single object. Data hiding refers to an object's ability to hide its data from code that is outside the object. Only the object's methods may directly access and make changes to the object's data. Classes and Objects Object reusability An object is not a stand-alone program, but is used by programs that its services.
An everyday example of an object Your alarm clock: An object The current second (0-59) The current minute (0-59) The current hour (1-12) 6 The time the alarm is set for (a valid hour and 1. Classes and Objects Fields are merely data values that define the state of the alarm clock is currently in. The user cannot directly manipulate these fields because they are private. To change a field's value, we must use one of the object's methods.
Some of the alarm clock object's methods Set time These methods can be activated by users, who are outside the alarm clock. They are public methods. Set alarm time 7 1. Classes and Objects The alarm clock also has private methods, which are parts of the object's private, internal workings.
External entities (such as you, the user of the alarm clock) do not have directly access to the alarm clock's private methods. The object is designed to execute these methods automatically and hide the details from you. Some of the alarm clock object's private methods Increment the current second Increment the current minute 8 1. Classes and Objects Before an object is created, it must be designed by a programmer.
The programmer determines the fields and the methods that are necessary, and then creates a class. Think of a class as a “blueprint” that objects may be created from. A class is not an object, but it is a description of an objects. When the program is running, it can use the class to create, in memory, as many objects as needed.
Each object that is created from a class is called an 9 instance of the class. Classes and Objects The housefly object is an housefly instance of the Insect class. It object has the fields and methods described by the Insect class. The Insect class describes Insect the fields and methods class that a particular type of object may have.
The mosquito object is an mosquito instance of the Insect class. It object has the fields and methods described by the Insect class. All of the objects that are created from the same class will have the fields ad methods described by the class. 10 Objects versus Primitive Variables Java primitive data types: byte, short, int, long, char, float, double, boolean Primitive variables are storage locations in the computer's memory.
A primitive data type is called “primitive” because a variable created with a primitive data type has no built-in capabilities other than storing a value. 11 Objects versus Primitive Variables String class allows you to create String objects In addition to storing strings, String object s have numerous methods that perform operations on the strings they hold. String name = “Peter”; A String object The name variable holds the address of address “Peter” a String object. 12 Building a Simple Class Step by Step Write a class named Rectangle.
Each object that is created from the Rectangle class will be able to hold data about a rectangle. A Rectangle object will have the following fields length: Hold the rectangle's length width: Hold the rectangle's width The Rectangle class will also have the following methods setLength: Store a value in an object's length field 13 Building a Simple Class Step by Step getWidth: Return the value in an object's width field. getArea: Return the area of the rectangle. When designing a class it is often helpful to draw a UML diagram.
UML stands for Unified Modeling Language. It provides a set of standard diagrams for graphically depicting object oriented systems. 14 UML Diagram for a Class The general layout of a UML diagram for a class: The diagram is a box that is divided into three sections. The top section is where you write the name of the class.
The middle section holds a list of the class's fields. The bottom section holds a list of the class's methods. 15 UML Diagram for a Class UML diagram for the Rectangle class Rectangle length width setLength() SetWidth() GetLength() GetWidth() getArea() 16 Writing the Code for a Class Create a file named Rectangle.java In the Rectangle.java file, we start by writing a general class skeleton as follows public class Rectangle { } The public access specifier indicates that the class will be publicly available to code outside the17 Writing the Code for the Class Fields We will write the code for the class's two fields width and length. We use variables of the double data types.
public class Rectangle { private double length; private double width; } 18 Writing the Code for the Class Fields Access specifier private: When the private access specifier is applied to a class member, the member cannot be accessed by the code outside the class. The member can be accessed only by methods that are members of the same class. public: When the public access specifier is applied to a class member, the member can be accessed by the code inside the class or outside. 19 Writing the setLength Method public class Rectangle { private double length; private double width; /** The setLength method stores a value in the length field.
@param len The value to store in length. */ public void setLength(double len) { length = len; } } 20 Using Rectangle Class 21 Using Rectangle Class 22 Using Rectangle Class 23 Using Rectangle Class 24 Using Rectangle Class Rectangle box = new Rectangle(); A Rectangle object The box variable length 0.0 holds the address of a Rectangle address width0.0); A Rectangle object The box variable 10. length 0 holds the address of address a Rectangle bject.0); A Rectangle object The box variable 10. length holds the address of 0 a Rectangle address 20.
0 Accessor and Mutator Methods It is a common practice to make all of a class's fields private and to provide public methods for accessing and changing those fields. A method that gets a value from a class's field but does not change it is known an accessor method (getter). A method that stores a value in a field or changes the value of a field is known as a mutator method (setter). In the Rectangle class getLength, getWidth: accessor methods setLength, setWidth: mutator methods 26 Avoiding Stale Data In the Rectangle class, the getArea method returns the result of a calculation.
The area of the rectangle is not stored in a field. Why? The area is not stored in a field because it could potentially become stale. When the value of an item is dependent on other data and that item is not updated when the other data is changed, it is said that the item has become stale. 27 Avoiding Stale Data If the area is stored in a field, the value of this field becomes incorrect as soon as either the length or width fields changed.
When designing a class, you should take care not to store in a field calculated data that can potentially become stale. Instead, provide a method that returns the result of the calculation. 28 Showing Access Specification in UML Diagrams In a UML diagram, A – character before a member name indicates that it is private. A + character before a member name indicates that it is public.
Rectangle - length - width + setLength() + setWidth() + getLength() + getWidth() 29 + getArea() Data Type and Parameter Notation in UML Diagrams The UML diagram also provides notation that you may use to indicate Data types of fields, methods Parameter variables Return type of a method Rectangle - length: double - width: double + setLength(len: double): void + setWidth(w: double): void + getLength(): double + getWidth(): double 30 + getArea(): double Layout of Class Members Typical layout of class members public class ClassName { Field declarations Methods definitions } 31 Checkpoint 6. Instance Fields and Methods Each instance of a class has its own set of fields, which are known as instance fields or instance variables. You can create several instances of a class and store different values in each instance's fields. The methods that operate on an instance of a class are known as instance methods.
Instance methods do not have the keyword static in their headers. Instance Fields and Methods Rectangle kitchen = new Rectangle(); Rectangle bedroom = new Rectangle(); Rectangle den = new Rectangle(); The box variable length 0.0 A holds the address of address Rectangle a Rectangle bject.0 object The bedroom 0.0 length A variable address Rectangle holds the address of width0.0 object a Rectangle bject. The den variable 0.0 length A holds the address of address Rectangle34 a Rectangle bject. Instance Fields and Methods kitchen.
variable Length Rectangle address 0 hold the address of width14. object a Rectangle object. 0 The bedroom variable 15. A hold the address of Length Rectangle address 0 a Rectangle width12.
The den variable 20. A holds the address of Length Rectangle35 address 0 a Rectangle bject.10 Assume that r1 and r2 are variables that reference Rectangle objects, and the following statements are executed: r1.0); Fill in the boxes in the figure that represent each object's length and width fields.0 A r1 address Rectangle width0.0 length A r2 address Rectangle width0.0 length A 0 r2 address Rectangle 15. Constructors A constructor Has the same name as the class. A method that is automatically called when an instance of a class is created.
Performs initialization or setup operations Storing initial values in instance fields. It is called a constructor because it helps construct an object. The constructor's header does not specify a 39 return type. Constructors Write a constructor in the class Rectangle: The constructor has the name Rectangle.
The constructor has two arguments The length of the rectangle The width of the rectangle Two arguments are then assigned to the length and width fields. Constructors 41 This constructor accepts two arguments. Constructors Constructor's header does not specify a return type – not even void.