1 objects and classes introduction to classes object variables and object references instantiating...

10
1 Objects and Classes • Introduction to Classes • Object Variables and Object References • Instantiating Objects • Using Methods in Objects • Reading for this Lecture: L&L, 3.1 - 3.2

Post on 19-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:

1

Objects and Classes

• Introduction to Classes

• Object Variables and Object References

• Instantiating Objects

• Using Methods in Objects

• Reading for this Lecture: L&L, 3.1 - 3.2

Page 2: 1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:

2

Introduction to Classes

• A class defines the attributes and behavior of a specific type of object– Attributes are variables declared in the class– Behaviors are methods defined in the class

• Normally, we access an object by calling a method defined by its class

• We may sometimes access an attribute defined by its class, but this is discouraged

Page 3: 1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:

3

Introduction to Classes

• A class has a name that we can use as if it were a data type when declaring a variable

• When we declare a variable with the name of a class as its type, we are creating an object variable (can contain a reference to an object)

• We access an object’s methods / attributes using the object variable name and the . notation, e.g. ClassName objectName; //object variable

objectName.methodName() // Note: The ( )

objectName.variableName // Note: No ( )

Page 4: 1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:

4

Example of a Class Definition

• We can draw a diagram of a class to outline its important features before writing code – its name, attributes, and behaviors

StateMachine

- state :int. . .

+ setState (int input): void. . .

Class Name

List of itsVariables

List of itsMethods

Page 5: 1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:

5

Example of a Class Definition

public class StateMachine {

// an attribute or variable

private int state;

// a behavior or method

public void setState (int input) {

state = input;

}

}

Page 6: 1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:

6

Example of a Class Definition

• Declaring a StateMachine object variable:StateMachine myStateMachine = … ;

• Accessing a myStateMachine method:myStateMachine.setState(1);

• Why can’t we just do this?myStateMachine.state = 1;

• Notice the word private in the declaration of the variable state?

Page 7: 1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:

7

Prototype for a Class Definition

• We use the Java reserved word private to prevent access to a variable or method from code that is written outside the class

• We use the Java reserved word public to allow access to a variable or method from code that is written outside the class

• Normally, we declare variables to be private• Normally, we declare methods to be public• We will see some valid exceptions later

Page 8: 1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:

8

Creating Objects, e.g. String class

• “String” is a commonly used class

• To declare a variable as an object reference, to a String, we use the class name of the object as the type name

String title;• This declaration does not create an object

• It only creates a variable that can hold a reference to a String object.

Page 9: 1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:

9

Creating Objects

• We use the new operator to create an object

• Creating an object is called instantiation• An object is an instance of a particular class• title is assigned a reference to a String object

that contains (encapsulates) the string “Moby Dick”

title = new String (“Moby Dick");

This calls the String constructor, which isa special method that sets up the object

Page 10: 1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:

10

Invoking Methods

• Once an object has been instantiated, we can use the dot operator to invoke or “call” any of the object’s methods

int count = title.length();• A method may return a value which can be

used in an assignment or expressionThe value of count will be set to 9

• A method invocation can be thought of as asking an object to perform a service