Chapter 9 Objects and Classes

Ace your homework & exams now with Quizwiz!

private modifier

The private modifier specifies that the member can only be accessed in its own class.

passing objects to methods

You can pass objects to methods. Like passing an array, passing an object is actually passing the reference of the object. The following code passes the myCircle object as an argument to the printCircle method:

class

You can put the two classes into one file, but only one class in the file can be a public class . Furthermore, the public class must have the same name as the file name. Therefore, the file name is TestCircle.java , since TestCircle is public. Each class in the source code is compiled into a .class file. When you compile TestCircle.java , two class files TestCircle.class and Circle.class are generated, as shown in Figure 9.5. client public class

Random class

- this class provided by Java is used to generate random numbers - part of the java.util package

A class is a construct that defines objects of the same type.

The Circle class is different from all of the other classes you have seen thus far. It does not have a main method, and therefore cannot be run; it is merely a definition for circle objects. The class that contains the main method will be referred to in this book, for convenience, as the main class .

Data Encapsulation

A private data field cannot be accessed by an object from outside the class that defines the private field. However, a client often needs to retrieve and modify a data field. To make a private data field accessible, provide a getter method to return its value. To enable a private data field to be updated, provide a setter method to set a new value. A getter method is also referred to as an accessor and a setter to a mutator . A getter method has the following signature:

protected modifier

Can be applied to individual data members and methods (but cannot be applied to a class); when applied, it restricts access to members of the same class and any derived class; e.g., the following method, setHashCode, is only accessible to members of the LibraryItem class and any derived classes (e.g., Book). package domain; public class LibraryItem { protected void setHashCode() { } } package domain; public class Book extends LibraryItem { public LibraryItem () { setHashCode(); } }

invoking objects data in methods

For example, myCircle.radius references the radius in myCircle and myCircle .getArea() invokes the getArea method on myCircle . Methods are invoked as operations on objects. The data field radius is referred to as an instance variable because it is dependent on a specific instance. For the same reason, the method getArea is referred to as an instance method because you can invoke it only on a specific instance. The object on which an instance method is invoked is called a calling object

The data fields can be of reference types. For example, the following Student class contains a data field name of the String type. String is a predefined Java class.

If a data field of a reference type does not reference any object, the data field holds a special Java value, null . null is a literal just like true and false . While true and false are Boolean literals, null is a literal for a reference type. The default value of a data field is null for a reference type, 0 for a numeric type, false for a boolean type, and \u0000 for a char type. However, Java assigns no default value to a local variable inside a method. The following code displays the default values of the data fields name , age , isScienceMajor , and gender for a Student object:

java.util.Date

In Listing 2.7, ShowCurrentTime.java, you learned how to obtain the current time using System.currentTimeMillis() . You used the division and remainder operators to extract the current second, minute, and hour. Java provides a system-independent encapsulation of date and time in the java.util.Date class, as shown in Figure 9.10.

OOP

Object Oriented Programming


Related study sets

Chapitre 3 - création de richesse par l'activité économique

View Set

Chapter 10: Small Business Promotion

View Set

Oregon 30-Hour Real Estate Finance Course Week 1

View Set

MCAT physics chap 9 atomic and nuclear phenoms 16%

View Set

Shipping, Freight and Materials Handling Terms - APEX

View Set