of 37
7/24/2019 Object Concepts jnn
1/37
Basic Object-Oriented concepts
7/24/2019 Object Concepts jnn
2/37
Concept: An object has behaviors
In old style programming, you had:
data, hich as completely passive
!unctions, hich could manipulate any data
An objectcontains both data and methods
that manipulate that data
An object is active,not passive" it doesthings
An object is responsible!or its on data
But: it can exposethat data to other objects
7/24/2019 Object Concepts jnn
3/37
Concept: An object has state
An object contains both dataand methods
that manipulate that data
#he data represent the stateo! the object
$ata can also describe the relationships beteen
this object and other objects
%&le: A CheckingAccountmight haveA balance'the internal state o! the account(
An owner'some object representing a person(
7/24/2019 Object Concepts jnn
4/37
%&le: A )*abbit+ object
ou could 'in a game, !or e&le( create an
object representing a rabbit
It ould have data:o hungry it is
o !rightened it is
.here it is
And methods:
eat, hide, run, dig
7/24/2019 Object Concepts jnn
5/37
Concept: Classes describe objects
%very object belongs to 'is an instanceo!( a
class
An object may have !ields, or variables
#he class describes those !ields
An object may have methods
#he class describes those methods
A class is li/e a template, or coo/ie cutter
7/24/2019 Object Concepts jnn
6/37
Concept: Classes are li/e
Abstract $ata #ypes An Abstract $ata #ype'A$#( bundles
together:
some data, representing an object or 0thing0
the operations on that data
%&le: a CheckingAccount, ith
operations deposit, withdraw, getBalance,etc1
Classes en!orce this bundling together
7/24/2019 Object Concepts jnn
7/37
%&le o! a class
class Employee { // fields String name; double salary; // a method oid pay !" { System#out#println!$%ay to the order of $ &
name & $ '$ & salary"; ((
7/24/2019 Object Concepts jnn
8/37
Appro&imate #erminology
instance 2 object
!ield 2 variable
method 2 !unction
sending a message to an object 2
calling a !unction
#hese are all approximatelytrue
7/24/2019 Object Concepts jnn
9/37
Concept: Classes !orm a hierarchy
Classes are arranged in a treeli/e structure called a
hierarchy
#he class at the root is named )b*ect %very class, e&cept )b*ect, has a superclass
A class may have several ancestors, up to )b*ect
.hen you de!ine a class, you speci!y its superclass I! you don3t speci!y a superclass, )b*ectis assumed
%very class may have one or more subclasses
7/24/2019 Object Concepts jnn
10/37
%&le o! 'part o!( a hierarchy
A +ileialogis a ialogis a -indowis a Container1
Container
%anel Scroll%ane -indow
ialog +rame
+ileialog
7/24/2019 Object Concepts jnn
11/37
C44 is di!!erent
In C44 there may be more than one root
but not in 5ava6
In C44 an object may have more than one
parent 'immediate superclass(
but not in 5ava6
5ava has a single, strict hierarchy
7/24/2019 Object Concepts jnn
12/37
Concept: Objects inherit !rom
their superclasses A class describes !ields and methods
Objects o! that class have those !ields and
methods
But an object alsoinherits:
the !ields described in the class7s superclasses
the methods described in the class7s superclasses
A class is nota complete description o! its
objects6
7/24/2019 Object Concepts jnn
13/37
%&le o! inheritance
class %erson { String name;
String age; oid birthday !" { age . age & ; (
(
class Employee e0tends %erson {
double salary; oid pay !" { ###((
%very Employeehas a name, age, and birthdaymethod as well asa salaryand a paymethod1
7/24/2019 Object Concepts jnn
14/37
Concept: Objects must be created
1 int n; does to things:it declares that n is an integer variable
it allocates space to hold a value !or n
1 Employee secretary; does onethingit declares that secretary is typeEmployee
1 secretary . new Employee ! "; allocatesthe space
7/24/2019 Object Concepts jnn
15/37
8otation: o to declare and
create objects Employee secretary; // declares secretary
secretary . new Employee !"; // allocates space
Employee secretary . new Employee!"; // both
But the secretary is still 0blan/0
secretary#name . $Adele$; // dot notation
secretary#birthday !"; // sends a message
7/24/2019 Object Concepts jnn
16/37
8otation: o to re!erence a
!ield or method Inside a class, no dots are necessary
class %erson { ### age . age & ; ###(
Outside a class, you need to say hich object
you are tal/ing to
if !*ohn#age 2 34" *ohn#birthday !";
I! you don7t have an object, you cannot use its
!ields or methods6
7/24/2019 Object Concepts jnn
17/37
Concept: thisobject
Inside a class, no dots are necessary, because
you are or/ing on thisobject
I! you ish, you can ma/e it e&plicit:
class %erson { ### this#age . this#age & ; ###(
1 thisis li/e an e&tra parameter to the method
ou usually don7t need to use this
7/24/2019 Object Concepts jnn
18/37
Concept: A variable can hold
subclass objects 9uppose B is a subclass o! A
5 Aobjects can be assigned to Avariables
5 B objects can be assigned to B variables
5 B objects can be assigned to A variables, but
5 Aobjects can notbe assigned to Bvariables
%very Bis also an Abutnot every A is a B
ou can cast: b6ariable . !B" a)b*ect;In this case, 5ava does a runtime chec/
7/24/2019 Object Concepts jnn
19/37
%&le: Assignment o!
subclassesclass og { ### (class %oodle e0tends og { ### (og myog;og roer . new og !";%oodle your%oodle;%oodle fifi . new %oodle !";
myog . roer; // ok
your%oodle . fifi; // okmyog . fifi; //okyour%oodle . roer; // illegalyour%oodle . !%oodle" roer; //runtime check
7/24/2019 Object Concepts jnn
20/37
Concept: ethods can be
overridden
9o birds can !ly1 %&cept penguins1
class Bird e0tends Animal { oid fly !String destination" { location . destination; ((
class %enguin e0tends Bird {
oid fly !String whateer" { ((
7/24/2019 Object Concepts jnn
21/37
Concept: $on7t call !unctions,
send messages Bird someBird . pingu;
someBird#fly !$South America$";
$id pinguactually go anyhere;ou sent the message fly!###"to pingu
I! pinguis a penguin, he ignored it
otherise he used the method de!ined in Bird
ou did notdirectly call any method
7/24/2019 Object Concepts jnn
22/37
9nea/y tric/: ou can still use
overridden methods
class +amily7ember e0tends %erson { oid birthday !" { super#birthday !"; // call oerridden method gie%resent !"; // and add your new stuff (
(
7/24/2019 Object Concepts jnn
23/37
Concept: Constructors ma/e objects
%very class has a constructorto ma/e its objects
7/24/2019 Object Concepts jnn
24/37
9ynta& !or constructors
Instead o! a return type and a name, just use
the class name
ou can supply arguments
Employee !String the8ame, double theSalary" {
name . the8ame; salary . theSalary;(
7/24/2019 Object Concepts jnn
25/37
#ric/:
7/24/2019 Object Concepts jnn
26/37
Internal or/ings:
Constructor chaining I! an Employeeis a %erson, and a %ersonis an
)b*ect, then hen you say new Employee !"#he Employeeconstructor calls the %ersonconstructor
#he %ersonconstructor calls the )b*ectconstructor
#he )b*ectconstructor creates a ne )b*ect
#he %ersonconstructor adds its on stu!! to the )b*ect
#he Employeeconstructor adds its on stu!! to the%erson
7/24/2019 Object Concepts jnn
27/37
#he case o! the vanishing
constructor I! you don7t rite a constructor !or a class, 5ava
provides one 'the default constructor(
#he one 5ava provides has no arguments I! you rite anyconstructor !or a class, 5ava
does notprovide a de!ault constructor
Adding a per!ectly good constructor can brea/a constructor chain
ou may need to !i& the chain
7/24/2019 Object Concepts jnn
28/37
%&le: Bro/en constructor
chain
class %erson { String name;
%erson !String name" { this#name . name; ((class Employee e0tends %erson { double salary;
Employee ! " { // here 9aa tries to call new %erson!" but cannot find it; salary . :#4; ((
7/24/2019 Object Concepts jnn
29/37
>i&ing a bro/en constructor chain
9pecial synta&: super!###"calls the superclass constructor
.hen one constructor calls another, that call must be first
class Employee {double salary;
Employee !String name" { super!name"; // must be first
salary . :#4; ((
8o you can only create %mployees ith names
#his is !air, because you can only create ?ersons ith names
7/24/2019 Object Concepts jnn
30/37
#ric/: one constructor calling
another1 this!###"calls another constructor !or this same class
It is poor style to have the same code more than once
I! you call this!###", that call must be the first thing in yourconstructor
class Something {
Something !int 0, int y, int
7/24/2019 Object Concepts jnn
31/37
Concept: ou can control access
class %erson { public String name; priate String age; protected double salary; public oid birthday { age&&; ((
%ach object is responsible !or its on data
Access control lets an object protect its data
.e ill discuss access control shortly
7/24/2019 Object Concepts jnn
32/37
Concept: Classes themselves can
have !ields and methods
7/24/2019 Object Concepts jnn
33/37
%&le o! a class variable
class %erson { String name;
int age; static int population; %erson !String name" { this#name . name;
this#age . ; population&&; ((
7/24/2019 Object Concepts jnn
34/37
Advice: *estrict access
Alays, alwaysstrive !or a narro inter!ace
>ollo theprinciple o! in!ormation hiding:
the caller should /no as little as possible
about ho the method does its job
the method should /no little or nothing about
here or hy it is being called a/e as much as possible priate
7/24/2019 Object Concepts jnn
35/37
Advice:
7/24/2019 Object Concepts jnn
36/37
@inds o! access
5ava provides !our levels o! access:
5 public: available everyhere
5 protected: available ithin the pac/age 'in thesame subdirectory( and to all subclasses
de!ault: available ithin the pac/age
5 priate: only available ithin the class itsel! #he de!ault is calledpac/agevisibility
In small programs this isn7t important111right;
7/24/2019 Object Concepts jnn
37/37
#he %nd