Unformatted text preview:

Objects and classeshandout for CS 302 by Will Benton (willb@cs)objects-and-classes.graffle: Created on Sun Jun 25 2006; modified on Mon Jan 29 2007; page 1 of 2Copyright © 2006 William C. BentonJava is an object-oriented language.This means that your Java programs will model problem-domain entities as objects, or collections of data and operations on that data. Your programs will solve problems by manipulating and combining objects.What is an object? At a low level, an object is a collection of data and a collection of operations on that data. More abstractly, it is a representation of some problem-domain entity.What is a class? A class is a "template" for a kind of objects, which we'll call instances of that class. (Some people call classes "object factories;" this image may also be helpful.)The class provides declarations for the fields contained in every instance (the data) as well as for the methods that can be invoked on each instance of this class (the operations).What does the following line of code do? This line of code declares a variable called n with type "reference to Name," It also creates a new instance of the Name class, and gives a reference to the newly-allocated Name to n./** This class represents a person's name. */public class Name { private String salutation; private String first; private String last; public Name(String salutation, String first, String last) { this.salutation = salutation; this.first = first; this.last = last; } public String friendlyGreeting() { return "Hello, " + first; } public String formalGreeting() { String full = salutation + " " + first + " " + last; return "Hello, " + full; } public String phonebookEntry() { String lastUpper = last.toUpperCase(); return lastUpper + ", " + first; }}Name n = new Name("Mr.", "Clark", "Kent");Can you explain that process a little more fully? Certainly. We already know what a variable declaration looks like, right?Yes, it's a Java statement consisting of a type, an identifier, and an optional initialization; for example, int k; or int z = 4;That's right. When you're creating an instance of a particular class, you declare a variable with that class name as a type....Ah, but declaring a variable with type Name really means that it holds a reference to some Name, right?Yes, please do keep that in mind. Once you've declared a variable of type "reference to Name," you can then create a new instance of Name by using what Java programmers call "operator new." Operator new does three things:Please refer to the following class declaration in order to follow the dialogue.1. It allocates a chunk of memory large enough to hold an instance of the class in question. Think of this as clearing off space on your desk for a new gadget.2. It clears the fields of the newly allocated instance, setting each to the default value for its type. (See the next page for more on default values.)3. It invokes a user-supplied constructor method, which will generally do some tasks to set up the fields of the newly-allocated object.So the example you asked about would construct a new instance of class Name with the values "Mr.", "Clark", and "Kent" for the salutation, first, and last fields, respectively.Super, man. What kind of value is "Mr.", by the way?Glad you asked! A sequence of characters enclosed in double-quotes is called a string literal; it is a value of type "reference to String." Since String objects are extremely common in real programs, Java provides a convenient way to specify literal strings.So String is also a class? Yes, but String is a special kind of class.falseboolean0nullreference typesDefault values for fields of various types:bytecharshortintlong0.0floatdoubleSo String is also a class? Yes, but String is a special kind of class.objects-and-classes.graffle; page 2 of 2Copyright © 2006 William C. BentonIn retrospect, I should have realized that. After all, the Java naming conventions dictate that the first letter of a class name should be capitalized.Indeed, they do.How is String special? For one, you can specify literal strings -- you can't do that with instances of other classes, like Name. The String class also supports a special operator, +, which is used to construct a new String by concatenating two existing Strings together.falseboolean0nullreference typesDefault values for fields of various types:bytecharshortintlong0.0floatdoubleOK, that's a little clearer now. Back to Name, though -- what are the the fields in class Name called?Name has three instance fields: salutation, first, and last.It looks like field declarations contain the keyword private, a type, and an identifier. Does private mean "field?"Not quite. private is a visibility modifier. We use visibility modifiers to restrict the ways in which other programmers can use the data and functionality in our objects.If a field is declared private, it will only be visible within methods declared in the same class. If, on the other hand, a field is declared public, it will be accessible to all other methods in the program.So we've declared class Name as public so the rest of our program will be able to use its functionality?Yes. Note that all of the methods are declared public, but all of the fields are declared private. This means that other parts of the program will be able to access the methods, but only the methods declared in Name will be able to directly access instance fields of Name.Is it very common to declare fields as private? It's extremely common. In fact, most Java programmers (and your CS 302 graders) recommend always declaring instance fields as private. Think of this practice as putting a "no user-servicable parts inside" sign on the outside of your classes.Ah, I see. So I can use private to ensure that I control how the instance data of a class is accessed?"Data" is a plural noun, but you're right otherwise.OK. Precisely why do I want to control how my data are accessed?Java programmers will often talk about "information hiding" and "encapsulation." These are features of a good object-oriented design. We want programmers to interact with objects by sending messages, not by accessing fields. If the details of which fields are used in your classes are hidden from other programmers, then they won't be tempted to depend on the details of how you've designed the internals of your class (and you'll be


View Full Document

UW-Madison COMPSCI 302 - Objects and Classes

Download Objects and Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Objects and Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Objects and Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?