Escolar Documentos
Profissional Documentos
Cultura Documentos
1- Introduction.
2- What is OOP.
3- OOP Vocabulary .
4- Creating Our First Class Object.
encapsulation
Rather, all communication is through messages.
(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
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;
OOP Vocabulary
MyClass boop = new MyClass();
state
behavior
identity
Java ICopyright 2000 Tom Hunter
OOP Vocabulary
state
behavior
identity
Each object in OOP has three key characteristics:
What?
How?
Who?
OOP Vocabulary
state
behavior
identity
Key characteristics:
(What) What is the objects state?
(How) What is the objects behavior?
OOP Vocabulary
state
behavior
identity
OOP Vocabulary
state
behavior
identity
first_name
last_name
soc_sec_number
hourly_rate
current_vacation_time
Java ICopyright 2000 Tom Hunter
OOP Vocabulary
state
behavior
identity
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.
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);
OOP Vocabulary
state
behavior
My class is:
identity
HourlyEmployee
OOP Vocabulary
state
behavior
identity
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);
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);
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);
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);
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);
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 );
Class Scope
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.
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
OOP Vocabulary
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.
Inheritance
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)
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 )
OOP Vocabulary
When one class sends messages to another class, we say
it uses the class that receives its messages.
Use
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 )
OOP Vocabulary
When one class inherits from another class, we say it is
an is-a relationship.
inheritance ( is-a )
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.
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
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 .
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.
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.
OOP Vocabulary
Relationships Between Classes: containment
( In the use relationship, it calls methods of another object.)
Order
message
Account
Order
Account
Java ICopyright 2000 Tom Hunter
OOP Vocabulary
Relationships Between Classes: containment
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.
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.
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.
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
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? );
JLabel howdy;
howdy = new JLabel( How Are Ya? );
howdy
How are Ya?
howdy
hello
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.)
OOP Vocabulary
Controlling Access to Member Variables and
Methods: public & private
private
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...
OOP Vocabulary
Controlling Access to Member: package
if you dont specify either public or private for any
feature
[ meaning class, method or variable ]
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.
Access Levels
Modifier
Public
Protected
no
modifier
Private
Objects Passed By
Reference
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.
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.
Creating Our
First
Class Object
Class Object Constructor static
The this
Reference