Mais conteúdo relacionado
Semelhante a OOP Basics (20)
OOP Basics
- 1. Great Introduction to OOP
Prepared by : Tom Hunter
Edited and Updated By:Mohamed Shahpoup
Java I—Copyright © 2000 Tom Hunter
- 2. 1- Introduction.
2- What is OOP.
3- OOP Vocabulary .
4- Creating Our First Class Object.
5- Using this Keyword.
Java I—Copyright © 2000 Tom Hunter
- 3. 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 I—Copyright © 2000 Tom Hunter
- 4. The Genius of Using Objects
• Dell didn’t design its own
motherboards.
• Compaq didn’t engineer its own
hard drives or operating systems.
• They bought the pieces and let somebody else do
the engineering.
Java I—Copyright © 2000 Tom Hunter
- 5. 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 I—Copyright © 2000 Tom Hunter
- 6. The Genius of Using Objects
• Dell doesn’t 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 I—Copyright © 2000 Tom Hunter
- 7. The Genius of Using Objects
• Dell is only exposed to the end result.
• Most important is the power supply’s public face—
the power.
• Dell doesn’t care how it works internally.
Java I—Copyright © 2000 Tom Hunter
- 8. The Genius of Using Objects
• Likewise, in OOP, you only care about what the
objects expose.
• You can’t know how somebody else’s object
works.
Java I—Copyright © 2000 Tom Hunter
- 9. The Genius of Using Objects
• You don’t care how the JOptionPane works.
• You care about its public methods—its “interface”.
You only care about its
public methods !
Java I—Copyright © 2000 Tom Hunter
- 10. 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 I—Copyright © 2000 Tom Hunter
- 11. The Genius of Using Objects
• In OOP, you put data structures first,
and then look at the algorithms that
operate on the data.
Java I—Copyright © 2000 Tom Hunter
- 12. 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 done—but that task
isn’t the job of that object—then that object asks
another object to do the task.
“If I can’t do it, then I’ll
ask somebody who can.”
Java I—Copyright © 2000 Tom Hunter
- 13. The Genius of Using Objects
• Again, since the first object can’t 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 I—Copyright © 2000 Tom Hunter
- 14. 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 I—Copyright © 2000 Tom Hunter
- 15. 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 I—Copyright © 2000 Tom Hunter
- 16. 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 I—Copyright © 2000 Tom Hunter
- 18. 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 I—Copyright © 2000 Tom Hunter
- 19. 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 doesn’t exist.
Java I—Copyright © 2000 Tom Hunter
- 20. 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 I—Copyright © 2000 Tom Hunter
- 21. state
behavior
identity
Java I—Copyright © 2000 Tom Hunter
- 22. OOP Vocabulary
state
behavior
identity
• Each object in OOP has three key characteristics:
What?
How?
Who?
Java I—Copyright © 2000 Tom Hunter
- 23. OOP Vocabulary
state behavior identity
• Key characteristics:
(What) What is the object’s state?
(How) What is the object’s behavior?
(Who) What is the object’s identity?
Java I—Copyright © 2000 Tom Hunter
- 24. 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 state—or current values—for 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 I—Copyright © 2000 Tom Hunter
- 25. 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 I—Copyright © 2000 Tom Hunter
- 26. 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 I—Copyright © 2000 Tom Hunter
- 27. 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 I—Copyright © 2000 Tom Hunter
- 28. 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 I—Copyright © 2000 Tom Hunter
- 29. OOP Vocabulary
state behavior identity
• My class is: HourlyEmployee
• Let’s 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 I—Copyright © 2000 Tom Hunter
- 30. OOP Vocabulary
state behavior identity
• My class is: HourlyEmployee
• I have instantiated HourlyEmployee.
• My instance is called joseph.
• The identity of my instance is joseph.
Java I—Copyright © 2000 Tom Hunter
- 31. 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 I—Copyright © 2000 Tom Hunter
- 32. OOP Vocabulary identity for each of the three.
Tell me the
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 I—Copyright © 2000 Tom Hunter
- 33. 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 I—Copyright © 2000 Tom Hunter
- 34. OOP Vocabulary behaviors for each of the three.
Tell me the
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 I—Copyright © 2000 Tom Hunter
- 35. 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 I—Copyright © 2000 Tom Hunter
- 36. OOP Vocabularythe state for each of the three.
Tell me
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 I—Copyright © 2000 Tom Hunter
- 37. 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 I—Copyright © 2000 Tom Hunter
- 38. 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 I—Copyright © 2000 Tom Hunter
- 40. OOP Vocabulary
Class Scope
• A class’s 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 I—Copyright © 2000 Tom Hunter
- 41. 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 I—Copyright © 2000 Tom Hunter
- 43. 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 didn’t use the word “extends” in your
class definition, you were still always extending
Object by default.
Java I—Copyright © 2000 Tom Hunter
- 44. 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 I—Copyright © 2000 Tom Hunter
- 46. 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 I—Copyright © 2000 Tom Hunter
- 47. 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 I—Copyright © 2000 Tom Hunter
- 48. OOP Vocabulary
• When one class sends messages to another class, we say
it “uses” the class that receives its messages.
Use
Java I—Copyright © 2000 Tom Hunter
- 49. 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 I—Copyright © 2000 Tom Hunter
- 50. OOP Vocabulary
• When one class inherits from another class, we say it is
an “is-a” relationship.
inheritance ( “is-a” )
Java I—Copyright © 2000 Tom Hunter
- 51. 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 I—Copyright © 2000 Tom Hunter
- 52. 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.
Account
Order message
• In other words, Order uses Account when
Order calls methods of Account.
Java I—Copyright © 2000 Tom Hunter
- 53. 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 I—Copyright © 2000 Tom Hunter
- 54. OOP Vocabulary
Relationships Between Classes: use
• Design Tip:
Avoid the “use” relationship whenever
you can. If you “use” somebody else’s class,
then any changes to that class can break your
class.
Java I—Copyright © 2000 Tom Hunter
- 55. 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 I—Copyright © 2000 Tom Hunter
- 56. OOP Vocabulary
Relationships Between Classes: containment
( In the use relationship, it calls methods of another object.)
Account
Order message
( In the containment relationship, it contains another object.)
Order
Account
Java I—Copyright © 2000 Tom Hunter
- 57. 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 I—Copyright © 2000 Tom Hunter
- 58. 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 I—Copyright © 2000 Tom Hunter
- 59. 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 I—Copyright © 2000 Tom Hunter
- 60. 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 I—Copyright © 2000 Tom Hunter
- 61. OOP Vocabulary
Relationships Between Classes
• These three relationships between classes form the
foundation of Object-Oriented Design.
use
“has-a”
“is-a”
Java I—Copyright © 2000 Tom Hunter
- 62. Techniques
for
Using Objects
Java I—Copyright © 2000 Tom Hunter
- 63. 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 I—Copyright © 2000 Tom Hunter
- 64. JLabel howdy; = new JLabel( “How Are Ya?” );
howdy
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 I—Copyright © 2000 Tom Hunter
- 65. 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 I—Copyright © 2000 Tom Hunter
- 66. Controlling
Access
to
Methods and Variables
Java I—Copyright © 2000 Tom Hunter
- 67. OOP Vocabulary
Controlling Access to Methods: public
• public—this 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 I—Copyright © 2000 Tom Hunter
- 68. 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 I—Copyright © 2000 Tom Hunter
- 69. OOP Vocabulary
Controlling Access to Member: package
• package—if you don’t 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 definition—this
change is transparent. It has zero effect.
H O W E V E R...
Java I—Copyright © 2000 Tom Hunter
- 70. OOP Vocabulary
Controlling Access to Member: package
• if you don’t specify either public or private for any
feature…
[ meaning class, method or variable ]
can be accessed by all methods in the
same package!
Java I—Copyright © 2000 Tom Hunter
- 71. 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 I—Copyright © 2000 Tom Hunter
- 72. OOP Vocabulary
Creating a Package
• A package is a way to organize classes.
• Normally, you create a public class.
• If you don’t define your class as public, then it’s only
accessible to other classes in the same package.
Java I—Copyright © 2000 Tom Hunter
- 73. Access Levels
Modifier Class Package Subclass World
Public Y Y Y Y
Protected Y Y Y N
no Y Y N N
modifier
Private Y N N N
Java I—Copyright © 2000 Tom Hunter
- 75. 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 I—Copyright © 2000 Tom Hunter
- 76. 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 I—Copyright © 2000 Tom Hunter
- 77. Creating Our
First
Class Object
Class – Object – Constructor – static
Java I—Copyright © 2000 Tom Hunter
- 79. 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 neighbor’s Ferrarri, you
would naturally say “Jane’s Ferrarri….”
• Likewise, it would be perfectly natural for you to refer
to the car you were sitting in as “this Ferrarri….”
Java I—Copyright © 2000 Tom Hunter
- 80. 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 itself—called
the this reference.
Java I—Copyright © 2000 Tom Hunter
- 81. 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
Applet’s 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 I—Copyright © 2000 Tom Hunter
Notas do Editor
- White Space Characters
- White Space Characters
- White Space Characters
- White Space Characters
- White Space Characters
- White Space Characters
- White Space Characters
- White Space Characters
- White Space Characters
- White Space Characters
- White Space Characters
- White Space Characters
- White Space Characters
- White Space Characters