Chapter 3 Objects and Classes Object Oriented Object Oriented - - PDF document

chapter 3
SMART_READER_LITE
LIVE PREVIEW

Chapter 3 Objects and Classes Object Oriented Object Oriented - - PDF document

Chapter 3 Objects and Classes Object Oriented Object Oriented Programming Programming OOP is a relatively new approach to programming which supports the creation of new data types and operations to manipulate those types. 2 What is


slide-1
SLIDE 1

Chapter 3

Objects and Classes

2

OOP is a relatively new approach to

programming which supports the creation of new data types and operations to manipulate those types.

Object Oriented Programming Object Oriented Programming

3

What is this Object ? What is this Object ?

There is no real

answer to the question, but we’ll call it a “thinking cap”.

The plan is to

describe a thinking cap by telling you what actions can be done to it.

slide-2
SLIDE 2

4

Using the Object’s Slots Using the Object’s Slots

You may put a piece of

paper in each of the two slots (green and red), with a sentence written on each.

You may push the green

button and the thinking cap will speak the sentence from the green slot’s paper.

And same for the red

button.

5

Example Example

6

Example Example

That test was a breeze !

slide-3
SLIDE 3

7

Example Example

I should study harder !

8

Thinking Cap Implementation Thinking Cap Implementation

We can implement

the thinking cap using a data type called a class.

public class ThinkingCap { . . . }

9

Thinking Cap Implementation Thinking Cap Implementation

The class will have

two components called greenWords and redWords. These components are strings which hold the information that is placed in the two slots.

Using a class permits

two features . . . public class ThinkingCap { String greenWords; String redWords; . . . }

slide-4
SLIDE 4

10

Thinking Cap Implementation Thinking Cap Implementation

The two components will be private instance

  • variables. This ensures

that nobody can directly access this information. The only access is through methods that we provide for the class. public class ThinkingCap { private char greenWords; private char redWords; . . . }

11

Thinking Cap Implementation Thinking Cap Implementation

In a class, the methods which manipulate the class are also listed.

class ThinkingCap { private char greenWords; private char redWords; . . . }

Implementations of Implementations of the thinking cap the thinking cap methods go here. methods go here.

12

Thinking Cap Implementation Thinking Cap Implementation

public class ThinkingCap { private char greenWords; private char redWords; public void slots(String newGreen, String newRed)... public void pushGreen( )... public void pushRed( )... }

Our thinking cap has at least three methods: Our thinking cap has at least three methods:

slide-5
SLIDE 5

13

Thinking Cap Implementation Thinking Cap Implementation

package myclassses.325; public class ThinkingCap { private char greenWords; private char redWords; public void slots(String newGreen, String newRed)... public void pushGreen( )... public void pushRed( )... }

The code for a new class is generally put in a Java The code for a new class is generally put in a Java package, as shown here: package, as shown here:

This means that ThinkingCap.java and ThinkingCap.class files will be in a subdirectory myclasses/325

14

CLASSPATH Environment Variable

Java searches for class files (including package files) in the directories referenced by the CLASSPATH environment variable

15

Using the Thinking Cap Using the Thinking Cap

A program

that wants to use the thinking cap can import the ThinkingCap class.

import myclasses.325.ThinkingCap; ...

slide-6
SLIDE 6

16

Package Visibility Rules

Anything declared private can only be

accessed from within the same class

Anything declared public can be accessed

from anywhere inside or outside the package

Anything not declared public or private can

be accessed from within the same package

Top-level classes cannot be declared

private

17

Using the Thinking Cap Using the Thinking Cap

Just for fun, the

example program will declare two ThinkingCap variables named student and fan.

import myclasses.325.ThinkingCap; public class Example { public static void main( ) { ThinkingCap student; ThinkingCap fan;

18

Using the Thinking Cap Using the Thinking Cap

The variables are

examples of reference variables, which means that they have the capability

  • f referring to

ThinkingCap

  • bjects that we

create with the new

  • perator.

import myclasses.325.ThinkingCap; public class Example { public static void main( ) { ThinkingCap student; ThinkingCap fan; student = new ThinkingCap( ); fan = new ThinkingCap( );

slide-7
SLIDE 7

19

Using the Thinking Cap Using the Thinking Cap

Once the

ThinkingCaps are created, we can activate methods such as slots for the student ThinkingCap.

import myclasses.325.ThinkingCap; public class Example { public static void main( ) { ThinkingCap student; ThinkingCap fan; student = new ThinkingCap( ); fan = new ThinkingCap( ); student.slots( "Hello", "Bye");

20

Using the Thinking Cap Using the Thinking Cap

The method activation consists of four parts, starting with the variable name.

student.slots( "Hello", "Bye"); Name of the variable Name of the variable

21

Using the Thinking Cap Using the Thinking Cap

The variable name is followed by a period.

student.slots( "Hello", "Bye"); A Period A Period

slide-8
SLIDE 8

22

Using the Thinking Cap Using the Thinking Cap

After the period is the name of the method that you are activating.

student.slots( "Hello", "Bye"); N a m e

  • f

t h e m e t h

  • d

N a m e

  • f

t h e m e t h

  • d

23

Using the Thinking Cap Using the Thinking Cap

Finally, the arguments for the method. In this example the first argument (newGreen) is "Hello" and the second argument (newRed) is "Bye".

student.slots( "Hello", "Bye" ); Arguments Arguments

24

A Quiz A Quiz

How would you activate student's pushGreen method ? What would be the

  • utput of student's

pushGreen method at this point in the program ?

public static void main(String[ ] args) { ThinkingCap student; ThinkingCap fan; student = new ThinkingCap( ); fan = new ThinkingCap( ); student.slots( "Hello", "Bye");

slide-9
SLIDE 9

25

A Quiz A Quiz

Notice that the

pushGreen method has

no arguments. At this point, activating

student.pushGreen will

print the string

Hello.

public static void main(String[ ] args) { ThinkingCap student; ThinkingCap fan; student = new ThinkingCap( ); fan = new ThinkingCap( ); student.slots( "Hello", "Bye"); student.pushGreen( );

26

A Quiz A Quiz

Trace through this program, and tell me the complete output.

public static void main(String[ ] args) { ThinkingCap student; ThinkingCap fan; student = new ThinkingCap( ); fan = new ThinkingCap( ); student.slots( "Hello", "Bye"); fan.slots( "Go Cougars!", "Boo!"); student.pushGreen( ); fan.pushGreen( ); student.pushRed( ); . . .

27

Thinking Cap Implementation Thinking Cap Implementation

public class ThinkingCap { private String greenWords; private String redWords; public void slots(String newGreen, String newRed)... public void pushGreen( )... public void pushRed( )... }

We will look at the body of slots, which must copy its We will look at the body of slots, which must copy its two arguments to the two private two arguments to the two private instance

instance variables.

variables.

slide-10
SLIDE 10

28

Thinking Cap Implementation Thinking Cap Implementation

public void slots(String newGreen, String newRed) { greenWords = newGreen; redWords = newRed; }

The method The method’ ’s implementation occurs after the s implementation occurs after the parameter list parameter list

29

Thinking Cap Implementation Thinking Cap Implementation

public void slots(String newGreen, String newRed) { greenWords = newGreen; redWords = newRed; }

Within the body of the method, the class’s instance variables and other methods may all be accessed.

30

Thinking Cap Implementation Thinking Cap Implementation

?

Within the body of the method, the class’ instance variables and

  • ther methods may all be accessed.

public void slots(String newGreen, String newRed) { greenWords = newGreen; redWords = newRed; } But, whose instance variables are these? Are they student.greenWords student.redWords fan.greenWords fan.redWords

slide-11
SLIDE 11

31

Thinking Cap Implementation Thinking Cap Implementation

Within the body of the method, the class’s instance variables and other methods may all be accessed.

public void slots(String newGreen, String newRed) { greenWords = newGreen; redWords = newRed; } If we activate student.slots: student.greenWords student.redWords

32

Thinking Cap Implementation Thinking Cap Implementation

public void pushGreen( ) { System.out.println(greenWords); }

Here is the implementation of the pushGreen method, which prints the green Words:

Notice how this method implementation uses the Notice how this method implementation uses the greenWords greenWords instance variable of the object. instance variable of the object.

33

public class ThinkingCap { private String greenWords; private String redWords; ... }

A Common Pattern A Common Pattern

Often, one or more methods will place

data in the instance variables...

  • ...so that other methods may use that data.

...so that other methods may use that data.

slots

pushGreen & pushRed

slide-12
SLIDE 12

34

Constructors

Look like methods without a return

value

Name is the same as the name of the

class

Must not be preceded by a return type

(not even void)

35

Constructors

A class may have any number of

constructors, each with a different list

  • f parameters.

Constructors initialize values of

instance variables

If no constructors are provided, the

system provides one default no arguments constructor

36

Method Types

Accessor Methods Provide information about the instance

variables

Mutator Methods Modify the instance variables

slide-13
SLIDE 13

37

“equals” Methods

An “equals” method is a programmer provided

method to test for equality other than by testing if the 2 variables refer to the same object

Typical format:

public boolean equals (Object obj) { if (obj instanceof …) {//test goes here } else return false; }

A default “equals” method is provided by the system

and works like “==“

38

toString Methods

In order to aid in output of the objects

a toString method can be provided

Typical Format:

public String toString ( ) { /* code to generate a printable string */ }

39

Static Fields and Methods

Static methods are methods that are

accesses through the class itself rather than through objects that are instances of the class

Static Fields are class member fields

whose value is the same for all instances

slide-14
SLIDE 14

40

Static Initializers

A static initializer is a code block that

initializes a static variable:

starts with the “static” keyword immediately follows the declaration of

the variable

41

The Keyw ord “this”

Used to refer to the current object Used in constructors to refer to other

constructors of the same class

42

javadoc

Javadoc is a program that can help with

documenting a java program by generating a companion document containing:

A list of methods with signature and return types A list of any comments starting with /** Comments may include the following

parameters:

@author @param @return @throws

slide-15
SLIDE 15

43

Sample javadoc Comments

44

Sample javadoc Output