You are on page 1of 7

Lesson Object Oriented Programming (OOP)

1. Object
An object is a software bundle of related state and behavior. Software
objects are often used to model the real-world objects that you nd in
everyday life. !his lesson e"#lains how state and behavior are re#resented
within an object$ introduces the conce#t of data enca#sulation$ and
e"#lains the benets of designing your software in this manner.
A Software Object
Software objects are conce#tually similar to real-world objects% they
too consist of state and related behavior. An object stores its state in
felds (variables in some #rogramming languages) and e"#oses its
behavior through methods (functions in some #rogramming languages).
&ethods o#erate on an object's internal state and serve as the #rimary
mechanism for object-to-object communication. (iding internal state and
re)uiring all interaction to be #erformed through an object's methods is
*nown as data encapsulation + a fundamental #rinci#le of object-oriented
#rogramming.
,icycle %
-
htt#%..docs.oracle.com.javase.tutorial.java.conce#ts.
2. Class
/n the real world$ you'll often nd many individual objects all of the
same *ind. !here may be thousands of other bicycles in e"istence$ all of
the same ma*e and model. 0ach bicycle was built from the same set of
blue#rints and therefore contains the same com#onents. /n object-
oriented terms$ we say that your bicycle is an instance of the class of
objects *nown as bicycles. A class is the blue#rint from which individual
objects are created.
!he following ,icycle class is one #ossible im#lementation of a bicycle%
class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedp(int incre!ent) {
speed = speed " incre!ent;
}
void applyBra#es(int decre!ent) {
speed = speed $ decre!ent;
}
void print%tates() {
%yste!&out&println('cadence(' "
cadence " ' speed(' "
speed " ' gear(' " gear);
}
}
!he synta" of the 1ava #rogramming language will loo* new to you$ but
the design of this class is based on the #revious discussion of bicycle
objects. !he elds cadence$ s#eed$ and gear re#resent the object's state$ and
the methods (change2adence$ change3ear$ s#eed4# etc.) dene its interaction
with the outside world.
5ou may have noticed that the ,icycle class does not contain a main
method. !hat's because it's not a com#lete a##lication6 it's just the
blue#rint for bicycles that might be used in an a##lication. !he
res#onsibility of creating and using new ,icycle objects belongs to some
other class in your a##lication.
7
htt#%..docs.oracle.com.javase.tutorial.java.conce#ts.
(ere's a ,icycle8emo class that creates two se#arate ,icycle objects and
invo*es their methods%
class Bicycle)e!o {
pu*lic static void !ain(%tring+, args) {
-- Create two di..erent
-- Bicycle o*/ects
Bicycle *i#e1 = new Bicycle();
Bicycle *i#e0 = new Bicycle();
-- 1nvo#e !ethods on
-- those o*/ects
*i#e1&changeCadence(20);
*i#e1&speedp(10);
*i#e1&changeGear(0);
*i#e1&print%tates();
*i#e0&changeCadence(20);
*i#e0&speedp(10);
*i#e0&changeGear(0);
*i#e0&changeCadence(30);
*i#e0&speedp(10);
*i#e0&changeGear(4);
*i#e0&print%tates();
}
}
!he out#ut of this test #rints the ending #edal cadence$ s#eed$ and gear
for the two bicycles%
cadence(20 speed(10 gear(0
cadence(30 speed(00 gear(4
9
htt#%..docs.oracle.com.javase.tutorial.java.conce#ts.
3. Inheritance
8i:erent *inds of objects often have a certain amount in common with
each other. &ountain bi*es$ road bi*es$ and tandem bi*es$ for e"am#le$ all
share the characteristics of bicycles (current s#eed$ current #edal
cadence$ current gear). 5et each also denes additional features that
ma*e them di:erent% tandem bicycles have two seats and two sets of
handlebars6 road bi*es have dro# handlebars6 some mountain bi*es have
an additional chain ring$ giving them a lower gear ratio.
Object-oriented #rogramming allows classes to inherit commonly used
state and behavior from other classes. /n this e"am#le$ ,icycle now
becomes the superclass of &ountain,i*e$ ;oad,i*e$ and !andem,i*e. /n the
1ava #rogramming language$ each class is allowed to have one direct
su#erclass$ and each su#erclass has the #otential for an unlimited number
of subclasses%
!he synta" for creating a subclass is sim#le. At the beginning of your class
declaration$ use the e"tends *eyword$ followed by the name of the class to
inherit from%
class 5ountainBi#e extends Bicycle {
-- new .ields and !ethods de.ining
-- a !ountain *i#e would go here
}
!his gives &ountain,i*e all the same elds and methods as ,icycle$ yet
allows its code to focus e"clusively on the features that ma*e it uni)ue.
<
htt#%..docs.oracle.com.javase.tutorial.java.conce#ts.
!his ma*es code for your subclasses easy to read. (owever$ you must
ta*e care to #ro#erly document the state and behavior that each
su#erclass denes$ since that code will not a##ear in the source le of
each subclass.
4. Interface
As you've already learned$ objects dene their interaction with the outside
world through the methods that they e"#ose. &ethods form the object's
interface with the outside world6 the buttons on the front of your television
set$ for e"am#le$ are the interface between you and the electrical wiring
on the other side of its #lastic casing. 5ou #ress the =#ower= button to turn
the television on and o:.
/n its most common form$ an interface is a grou# of related methods with
em#ty bodies. A bicycle's behavior$ if s#ecied as an interface$ might
a##ear as follows%
inter.ace Bicycle {
-- wheel revolutions per !inute
void changeCadence(int newValue);
void changeGear(int newValue);
void speedp(int incre!ent);
void applyBra#es(int decre!ent);
}
!o im#lement this interface$ the name of your class would change (to a
#articular brand of bicycle$ for e"am#le$ such as A2&0,icycle)$ and you'd
use the im#lements *eyword in the class declaration%
class A2&0,icycle implements ,icycle >
int cadence = 0;
int speed = 0;
int gear = 1;
-- 6he co!piler will now re7uire that !ethods
-- changeCadence8 changeGear8 speedp8 and applyBra#es
-- all *e i!ple!ented& Co!pilation will .ail i. those
-- !ethods are !issing .ro! this class&
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedp(int incre!ent) {
?
htt#%..docs.oracle.com.javase.tutorial.java.conce#ts.
speed = speed " incre!ent;
}
void applyBra#es(int decre!ent) {
speed = speed $ decre!ent;
}
void print%tates() {
%yste!&out&println('cadence(' "
cadence " ' speed(' "
speed " ' gear(' " gear);
}
}
/m#lementing an interface allows a class to become more formal about
the behavior it #romises to #rovide. /nterfaces form a contract between
the class and the outside world$ and this contract is enforced at build time
by the com#iler. /f your class claims to im#lement an interface$ all
methods dened by that interface must a##ear in its source code before
the class will successfully com#ile.
5. Package
A #ac*age is a names#ace that organi@es a set of related classes and
interfaces. 2once#tually you can thin* of #ac*ages as being similar to
di:erent folders on your com#uter. 5ou might *ee# (!&L #ages in one
folder$ images in another$ and scri#ts or a##lications in yet another.
,ecause software written in the 1ava #rogramming language can be
com#osed of hundreds or thousands of individual classes$ it ma*es sense
to *ee# things organi@ed by #lacing related classes and interfaces into
#ac*ages.
!he 1ava #latform #rovides an enormous class library (a set of
#ac*ages) suitable for use in your own a##lications. !his library is *nown
as the =A##lication Programming /nterface=$ or =AP/= for short. /ts
#ac*ages re#resent the tas*s most commonly associated with general-
#ur#ose #rogramming. Aor e"am#le$ a String object contains state and
behavior for character strings6 a Aile object allows a #rogrammer to easily
create$ delete$ ins#ect$ com#are$ or modify a le on the lesystem6 a
Soc*et object allows for the creation and use of networ* soc*ets6 various
34/ objects control buttons and chec*bo"es and anything else related to
gra#hical user interfaces. !here are literally thousands of classes to
choose from. !his allows you$ the #rogrammer$ to focus on the design of
your #articular a##lication$ rather than the infrastructure re)uired to ma*e
it wor*.
B
htt#%..docs.oracle.com.javase.tutorial.java.conce#ts.
C
htt#%..docs.oracle.com.javase.tutorial.java.conce#ts.

You might also like