April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

34
April 3, 1998 CS102-02 Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3

Transcript of April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

Page 1: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Introduction to Object-Oriented Programming

CS 102-02

Lecture 1-3

Page 2: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Before You Write a Program...

• Decide on data– What input does your program need?– What data will it manipulate?– What information will it produce?

• Actions– Things your program does

Page 3: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Types of Data

• Related data– Automobiles– Whole numbers– Fractions– Sentences

Page 4: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

How Do Actions and Data Relate?

• Actions can act on data– Square root procedures act on numbers– Problem: Need different actions for different

data

• Actions and data can coexist– Objects combine both data and actions into one

package

Page 5: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Driving a Car

• When you step on the gas in a car, do you think:– “I’m calling the accelerate procedure and

passing it a pink Chevy Malibu and a pedal position.”

– Or, “Chevy Malibus ‘know’ how to accelerate already and I’m just asking the car to do its thing.”?

Page 6: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

The World is Full of Objects I

• Some objects in the world, and what they can do:

Object Actions

Chevy Malibus Stop, go, turn

Microscopes Focus, insert slide, remove slide

Hotels Make up rooms, check in guests

Page 7: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

The World is Full of Objects II

• Some objects in the world, and what they know:

Object Data

Chevy Malibus Gas level, coolant level, top speed

Microscopes Slide light on?

Hotels Number of conference rooms

Page 8: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

How are Objects Related?

So, Nat'ralists observe, a FleaHath smaller Fleas that on them prey, And these have smaller Fleas to bite 'em,And so proceed ad infinitum. -J. Swift

Page 9: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Object Relationships

• Objects can contain other objects– Composition– “Has a” (or “hath” if you’re an English author)

relationship

• Objects are more specific versions of other objects– Inheritance– “Is a” relationship

Page 10: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Inheritance

P la n es T ra in s

F o rd C h rys le r

M a libu

C h e vro le t

G M

A u tom ob iles

V e h ic les

Page 11: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Same Car, Different View

P la n es T ra in s

S p o rts C ar T ru ck

C h e vy M a libu

In e xp e ns ive

L e xu s L S 4 00

E xp en s ive

S ed an

A u tom ob iles

V e h ic les

Page 12: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

The Welcome Applet// A first program in Java

// import Applet class

import java.applet.Applet;

// import Graphics class

import java.awt.Graphics;

public class Welcome extends Applet {

public void paint( Graphics g ) {

g.drawString( "Welcome to Java Programming!", 25, 25 );

}

}

A “Welcome” is a kind of “Applet”

Page 13: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Reuse, Reuse, Reuse

• O-O concepts make it easy to reuse– Inheritance: Someone else creates the general,

and you add specifics– Composition: Put the puzzle pieces together

“Writing good software is hard, so avoid it whenever possible.”

- Me

Page 14: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Building an Airport

• What actions do airports know how to perform?

• What attributes do airports have?

• Are airports a kind of something?

• What kinds of airports are there?

• Do airports have logical subparts?

Page 15: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

“Just Like Summer Vacation, -- No Class”

• A class is a specification of :– Structure (the data, a.k.a. instance variables)

– Actions (methods)

– Inheritance (parents, or derived structure and actions)

for objects.

Page 16: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Examples of Classes

• Related groups of things constitute a class

• Share the same structure, actions (behavior) and similarly derived

– Aardvarks– Airports– Applets

Page 17: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Classes in Java

If you’ll need a group of related objects, create a class:

class Point { int x, y; }

Define a class with:

class classname {

Class definition (some data and/or some actions)

}

Page 18: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Classes Have Data• Airport class

– Gates– Runways– Airlines

• Class data goes inside the class definition, usually at the very beginning:public class Time1 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59

Page 19: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Classes Know Actions

• Classes aren’t just data, but actions too– At the airport

• Delivering baggage

• Preparing for plane’s arrival

– Class actions are called methods

Page 20: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Types

• Type is similar to class: a collection of data and actions

• Usually, we’ll consider type and class to be the same thing– In Java there are interfaces and classes

Page 21: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Abstract Data Types

• ADTs (from HTP 6.16) are implemented in Java with classes– An airport class represents the abstract notion

of a class– The Platonic “form”

Page 22: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Objects are Instances

• Classes are the overarching concepts– Concept “airport” is an abstract notion

• Objects are instances of those classes– O’Hare, LAX and Heathrow are concrete

instances of airports

Airport : O’Hare :: Class : Object

Page 23: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Creating an Object

Use new:

Airport peotone = new Airport(“Peotone, IL”);

Give the variable a name

What type of variable is it?

Some airport specification

You want a new what?

Page 24: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Java Object Magic

• Creating objects is easy in Java– Forget about memory ‘cuz Java’s simple

• Want another airport, just call new again!

Page 25: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Creating Instances from Classes

• Real-world– Spend money to hire construction crews– Lay asphalt– Build roads

• In Java, build airports with constructors– Special methods defined in a class which set up

new objects– Same name as class

Page 26: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Building Timepublic class Time1 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59

// Time1 constructor initializes each // instance variable to zero. Ensures // that each Time1 object starts in a // consistent state. public Time1() { setTime( 0, 0, 0 ); }

Anybody can create a new Time1 object

Constructors have the same name as the class

Page 27: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Hiding (a.k.a. Encapsulating) Data• Airport operations

– Do you know:• Outer marker?

• NDB?

• ATIS Frequency for O’Hare?

• Use the airport because you only need to know a little– Parking lot, ticket counter, baggage claim, ...

Page 28: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Why Hide Data?• Can’t break it

– What if you could change the tower frequency?– Double-check data

• Easier for you– What if you couldn’t get on a plane without

knowing how to operate a jetway?

• Inner workings can change– Change the guts of the airport, but don’t change

the ticket counter, baggage claim, ...

Page 29: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Hiding Data in Javapublic class Time1 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59

// Time1 constructor initializes each instance variable // to zero. Ensures that each Time1 object starts in a // consistent state.

public Time1() { setTime( 0, 0, 0 ); }

Nobody can mess with hour, minute or second

Nobody can set hour = “ABC” or minute = “456.45”

Page 30: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Hiding Timepublic class Time1 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59

// Time1 constructor initializes each // instance variable to zero. Ensures // that each Time1 object starts in a // consistent state. public Time1() { setTime( 0, 0, 0 ); }

Anybody can create a new Time1 object

One of our reasons for data hiding

Page 31: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Modular Design

• View related abstractions together

• Modules are useful only if they’re loosely connected– Passenger Terminal and Maintenance, not a

Door and its hinges

Passenger Terminal

Maintenance & Storage

Airport

Modules

Page 32: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Modularity in Java

• Java has two ways to separate modules:– Classes

• Use class to define

• Separate classes with access modifiers

– Packages• Group related classes together

• Optionally, declare a package using package in the first line of the file

package transportation.airport;• Without package, code is part of the default package

Page 33: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Object-Oriented Means… I• Objects: Combining data and actions under

one roof

• Hierarchies: An ranking of abstractions– Inheritance: The “is a” hierarchy– Composition: The “part of” hierarchy

• Abstraction: What distinguishes an object from other kinds objects, given a particular perspective

Page 34: April 3, 1998CS102-02Lecture 1-3 Introduction to Object-Oriented Programming CS 102-02 Lecture 1-3.

April 3, 1998 CS102-02 Lecture 1-3

Object Oriented Means… II

• Hiding data: Only the essentials are visible to the outside world

• Modularity: Group related abstractions together