Você está na página 1de 81

Great Introduction to OOP

Java ICopyright 2000 Tom Hunter

1- Introduction.
2- What is OOP.
3- OOP Vocabulary .
4- Creating Our First Class Object.

5- Using this Keyword.

Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


How did companies like Dell, Compaq and
Gateway get so big?

They bought components from other companies


and assembled the pieces into their products.

Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


Dell didnt design its own
motherboards.
Compaq didnt engineer its own
hard drives or operating systems.

They bought the pieces and let somebody else do


the engineering.
Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


Dell, Compaq and Gateway let somebody else
reinvent the power supply or the motherboard.
Object-Oriented programming is the same idea.

A program is composed of generic objects, with


certain standard properties, and certain standard
operations the objects can perform.
Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


Dell doesnt care how the power supply works.
Dell cares if the power supply works.
How the power supply works is hidden and private.
Only the end result is visible.
Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


Dell is only exposed to the end result.
Most important is the power supplys public face
the power.

Dell doesnt care how it works internally.

Java ICopyright 2000 Tom Hunter

The Genius of Using Objects

Likewise, in OOP, you only care about what the


objects expose.
You cant know how somebody elses object
works.

Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


You dont care how the JOptionPane works.
You care about its public methodsits interface.

You only care about its


public methods !

Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


In traditional procedural programming, you
search for verbs in the problem definition.
In procedural programming, the verbs directly
suggest procedures and then, lastly, you think of
data variables to go with those procedures.

Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


In OOP, you put data structures first,
and then look at the algorithms that
operate on the data.

Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


The secret to effective OOP:
Each object carries out a small set of related tasks.
If an object needs a task donebut that task
isnt the job of that objectthen that object asks
another object to do the task.

If I cant do it, then Ill


ask somebody who can.
Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


Again, since the first object cant do the

task, it asks the second object to carry out the


task.
In OOP jargon, we say:

A Client object sends a


message to a Server object.
Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


In OOP, one object must never directly
manipulate the internal data of another object.

encapsulation
Rather, all communication is through messages.

(A message is another name for a method call.)

Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


When you design your object to hide how it
handles requests...
(messages / method calls)
...you make it easily reusable.

Java ICopyright 2000 Tom Hunter

The Genius of Using Objects


When you see a Windows OS computer lock up,
and you do a CTRL-ALT-DEL, the Close
Program window that pops up might say:

(Not Responding)
That message means that some Windows object is
not responding to messages.
Some program called a method, but Windows
failed to respond. (No surprise)
Java ICopyright 2000 Tom Hunter

OOP Vocabulary

Class

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
class
The term class is the blueprint which the object is
actually made, or instantiated.
MyClass boop;
boop = new MyClass();
We are now familiar with this: The first MyClass
boop; makes a reference called boop.
Java ICopyright 2000 Tom Hunter

OOP Vocabulary
MyClass boop;

At this point, the reference called


boop does not actually point to any
existing object.
Soon, it will point to an object of type
MyClass, but now the object doesnt exist.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
MyClass boop = new MyClass();

When this statement executes, the new


keyword executes the default Constructor
for MyClass, which actually creates an object in
memory and assigns that reference to boop.
The handle to that just-created object is
given to the MyClass reference boop.
Now boop points to the new MyClass object.
Java ICopyright 2000 Tom Hunter

state

behavior
identity
Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state
behavior
identity
Each object in OOP has three key characteristics:
What?

How?
Who?

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state
behavior

identity

Key characteristics:
(What) What is the objects state?
(How) What is the objects behavior?

(Who) What is the objects identity?

Java ICopyright 2000 Tom Hunter

OOP Vocabulary

state

behavior

identity

All instances of a class have the same instance


variables, but of course those variables have
different values inside them.
The stateor current valuesfor an instance of a
class, is called the state of that class.
The current values of those variables define the
current situation or state of this instance of the
class.
Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state
behavior

identity

For example, if I have a class called


HourlyEmployee, then it contains instance
variables:

first_name
last_name
soc_sec_number
hourly_rate
current_vacation_time
Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state

behavior

identity

All objects that are instances of the same class


share the same behavior.
They all have the same methods.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state
behavior
identity
My class is:
HourlyEmployee
All instances of this class have these methods:
calculate_pay()
setName()
Methods
getName()
setSSN()
getSSN()
getVacationTime()
setVacationTime()
getHourlyRate()
setHourlyRate()
Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state
behavior
identity
My class is:
HourlyEmployee
Every example, or instantiation, of this class
has the same methods (behavior) available to
it.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state
behavior
identity
My class is: HourlyEmployee
Lets instantiate HourlyEmployee :
HourlyEmployee joseph; // empty reference.
joseph = new HourlyEmployee(Joe,Smith,
598-22-7893,$10.00,22.25);

Now, I have created an instance of the class


HourlyEmployee.
Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state

behavior

My class is:

identity

HourlyEmployee

I have instantiated HourlyEmployee.


My instance is called joseph.
The identity of my instance is joseph.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state
behavior

identity

The identity of my instance is


joseph.
The state of my instance is:
first_name = Joe
last_name = Smith
soc_sec_number = 598-22-7893
hourly_rate = $10.00
current_vacation_time = 22.25

The behavior of my instance is:


calculate_pay()
setName()
getName()
setSSN()
getSSN()
Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Tell me the identity for each of the three.
state
behavior
identity
Now, I will instantiate three objects:
HourlyEmployee marie;
marie = new HourlyEmployee(Mary,J.,
555-24-1516,$30.00,0);
HourlyEmployee theodore;
theodore = new HourlyEmployee(Ted,L.,
681-22-9875,$10.00,22);
HourlyEmployee david;
david = new HourlyEmployee(Dave,D.,
198-99-0098,$15.00,8);

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state
behavior
identity
Identity is the reference to this instantiation.
HourlyEmployee marie;
marie = new HourlyEmployee(Mary,J.,
555-24-1516,$30.00,0);
HourlyEmployee theodore;
theodore = new HourlyEmployee(Ted,L.,
681-22-9875,$10.00,22);
HourlyEmployee david;
david = new HourlyEmployee(Dave,D.,
198-99-0098,$15.00,8);

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Tell me the behaviors for each of the three.
state
behavior
identity
HourlyEmployee marie;
marie = new HourlyEmployee(Mary,J.,
555-24-1516,$30.00,0);
HourlyEmployee theodore;
theodore = new HourlyEmployee(Ted,L.,
681-22-9875,$10.00,22);
HourlyEmployee david;
david = new HourlyEmployee(Dave,D.,
198-99-0098,$15.00,8);

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state
behavior
identity
All three have the exact same behaviors.
HourlyEmployee marie;
marie = new HourlyEmployee(Mary,J.,
555-24-1516,$30.00,0);
HourlyEmployee theodore;
theodore = new HourlyEmployee(Ted,L.,
681-22-9875,$10.00,22);
HourlyEmployee david;
david = new HourlyEmployee(Dave,D.,
198-99-0098,$15.00,8);

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Tell me the state for each of the three.
state
behavior
identity
HourlyEmployee marie;
marie = new HourlyEmployee(Mary,J.,
555-24-1516,$30.00,0);
HourlyEmployee theodore;
theodore = new HourlyEmployee(Ted,L.,
681-22-9875,$10.00,22);
HourlyEmployee david;
david = new HourlyEmployee(Dave,D.,
198-99-0098,$15.00,8);

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state
behavior
identity
The state of each instance is defined by its
instance variables.
HourlyEmployee marie;
marie = new HourlyEmployee(Mary,J.,
555-24-1516,$30.00,0);
HourlyEmployee theodore;
theodore = new HourlyEmployee(Ted,L.,
681-22-9875,$10.00,22);
HourlyEmployee david;
david = new HourlyEmployee(Dave,D.,
198-99-0098,$15.00,8);
Java ICopyright 2000 Tom Hunter

OOP Vocabulary
state
behavior
identity
The state of an instance can only be changed by
going through its methods or behaviors.
HourlyEmployee marie;
marie = new HourlyEmployee(Mary,J.,
555-24-1516,$30.00,0);

marie.setSSN( 444-33-1264 );

Java ICopyright 2000 Tom Hunter

Class Scope

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Class Scope
A classs Instance variables and methods have a
thing called class scope.
Within the class (within the scope of that class), class
member variables are accessible by name. (static Members)
So, inside or outside of any method in that class, those
instance variables can be reached from anywhere in the
class.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Class Scope
If a member variable has been (foolishly) declared
public, then it can be accessed outside of the class by
simply referencing as follows:
ClassName.primitive_variable

ClassName.Object_variable.
Another instance of this class has access to the
instance variables in any other instance of this class.
You can use the instance identifier or the class name if
it is declared as a static variable.
Java ICopyright 2000 Tom Hunter

Cosmic Base Class

Java ICopyright 2000 Tom Hunter

OOP Vocabulary

Cosmic Base Class


In Java, all classes are built on other classes.
We say, that one class extends another class.

Ultimately, all classes in Java stem from one


central Cosmic Base Class called Object.
Even if you didnt use the word extends in your
class definition, you were still always extending
Object by default.
Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Base Class
When you extend any Base Class, the new
(derived) class has all the properties ( instance
variables) and methods of its parent, or Base Class.
You can choose to modify or keep any method of
the parent, or you can create methods that only
apply to the child or inherited class.

Java ICopyright 2000 Tom Hunter

Inheritance

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Inheritance
The concept of extending a base class is called
Inheritance.
Inheritance is the second fundamental concept of
Object-Oriented programming.
(Encapsulation is the first,
Polymorphism is the third)

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Relationships Between Classes
Classes can be related to each other in one of three
alternative ways:

use

containment ( has-a )
inheritance ( is-a )

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
When one class sends messages to another class, we say
it uses the class that receives its messages.

Use

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
When one class lives as an Instance Variable within
another class, we say it is Contained, a has-a
relationship.

Containment ( has-a )

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
When one class inherits from another class, we say it is
an is-a relationship.

inheritance ( is-a )

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Relationships Between Classes: use
Imagine that we have a class Order.
Class Order needs to use the class Account, in order
to check for credit status.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Relationships Between Classes: use
Generally, if a method of class Order
sends a message to an object of class Account,
then Order uses Account.

Order

message

Account

In other words, Order uses Account when


Order calls methods of Account.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Relationships Between Classes: use
Also, we say class Order uses class Account if:
A method of Order :
creates
receives or
returns
objects of class Account .

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Relationships Between Classes: use

Design Tip:
Avoid the use relationship whenever
you can. If you use somebody elses class,
then any changes to that class can break your
class.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Relationships Between Classes: containment
The Containment relationship
(also known as the Composition relationship)
is a special case of the use relationship.
In a Containment / Composition relationship,
at least one method of one class actually contains an
object of another class.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Relationships Between Classes: containment
( In the use relationship, it calls methods of another object.)

Order

message

Account

( In the containment relationship, it contains another object.)

Order

Account
Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Relationships Between Classes: containment

In a has-a relationship, a class becomes an


instance variable for the class we are defining.
public class Order extends Object
{
Account acct = new Account();

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Relationships Between Classes: inheritance
Inheritance means specialization.
When we inherit from a class, we wish to keep nearly
everything in the base class (Superclass).
In inheritance, we seek to elaborate on what we receive
from the Superclass.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Relationships Between Classes: inheritance
We start with the class Order.
Then, we wish to create a Subclass off of Order.
Our Subclass is called RushOrder.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Relationships Between Classes: inheritance
Class RushOrder has everything that Order has, but it:
-adds a few instance variables, maybe
-adds a method or two and
-overrides a method or two.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Relationships Between Classes
These three relationships between classes form the
foundation of Object-Oriented Design.

use
has-a
is-a
Java ICopyright 2000 Tom Hunter

Techniques
for
Using Objects

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Techniques for Using Objects
We have spent a lot of time emphasizing the difference
between a reference and the object to which it refers.

JLabel howdy;
howdy = new JLabel( How Are Ya? );

Java ICopyright 2000 Tom Hunter

JLabel howdy;
howdy = new JLabel( How Are Ya? );

howdy
How are Ya?

We start off by declaring a reference howdy to an object


of type JLabel.
Then, we instantiate the object by calling its constructor
with the new keyword, and assign the handle to this
instantiation to the reference we declared: howdy.
Java ICopyright 2000 Tom Hunter

howdy = new JLabel( How Are Ya? );

howdy

hello

How are Ya?

Okay, what happens when I do the following statement?


JLabel hello; // A new reference
hello = howdy;

Now, both references point to the exact same object.


Any changes made from howdy will be reflected in hello.
Java ICopyright 2000 Tom Hunter

Controlling
Access
to
Methods and Variables
Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Controlling Access to Methods: public
publicthis lets clients see the services (methods) the
class provides (which means view the interface.)

The interface is the collective name for all the


various methods that are available in the class.
Methods should be public.

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Controlling Access to Member Variables and
Methods: public & private
private

It hides implementation details.


Private data members (variables) are only accessible
through the public interface (Accessor methods)
using public methods. (getter and Setter).

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Controlling Access to Member: package
packageif you dont specify that a method or a
data variable is either private or public, then
when you have automatically given it
package access.
If your program has only one class definitionthis
change is transparent. It has zero effect.
H O W E V E R...

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Controlling Access to Member: package
if you dont specify either public or private for any
feature
[ meaning class, method or variable ]

can be accessed by all methods in the


same package!

Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Controlling Access to Member: package
So, if you have the following field in your class:
public class MyClass
{
int mySalary;

}
and your class is stored in java.util.*;
then any other method in any class that is also
stored in this package can change this variable
to anything it wants. No methods needed!
Java ICopyright 2000 Tom Hunter

OOP Vocabulary
Creating a Package
A package is a way to organize classes.
Normally, you create a public class.
If you dont define your class as public, then its only
accessible to other classes in the same package.

Java ICopyright 2000 Tom Hunter

Access Levels
Modifier

Class Package Subclass World

Public

Protected

no
modifier
Private

Java ICopyright 2000 Tom Hunter

Objects Passed By
Reference

Java ICopyright 2000 Tom Hunter

Object-Based Programming
Objects Passed By Reference
As we know the name or reference for an object
represents a memory location
where the object is stored.
When an object is passed, only the reference is passed.

Java ICopyright 2000 Tom Hunter

Object-Based Programming
Objects Passed By Reference
That means, only the address of the object is passed.
A copy is NOT made of the object.
This will have interesting implications.

Java ICopyright 2000 Tom Hunter

Creating Our
First
Class Object
Class Object Constructor static

Java ICopyright 2000 Tom Hunter

The this
Reference

Java ICopyright 2000 Tom Hunter

The this Reference


You were sitting in your Ferrarri in your driveway.
Next door, your plumber neighbor was sitting in her
Ferrarri.

If you wanted to refer to your neighbors Ferrarri, you


would naturally say Janes Ferrarri.
Likewise, it would be perfectly natural for you to refer
to the car you were sitting in as this Ferrarri.

Java ICopyright 2000 Tom Hunter

The this Reference


this Ferrarri.
In Java, the this reference is used to refer to the object
you are inside of at this moment.

We say that each object has a reference to itselfcalled


the this reference.

Java ICopyright 2000 Tom Hunter

The this Reference


The this reference is used to refer to both the instance
variables and methods of an object.
In Event Handlers, we have used the this reference to
show that this Applet (and by implication this
Applets actionPerformed method) will listen for
events from this object.
The this reference can also be used for cascading
method calls which allow a reference to be passed back
up the calling chain.
Java ICopyright 2000 Tom Hunter

Você também pode gostar