Review: class
- A class in
Java contains
(1) variables and
(2) methods
- The
purpose of
a class in
Java is:
- To
group (= package)
useful
methods
E.g.: the
Math class
in Java's library
- To
implement (= model)
objects
that
help in construct
the solution
E.g.: the
String class
in Java's library
|
|
Writing
complex computer programs
Writing
complex computer programs
-
Programming technique to
handle
large scale
programming prjects
before
1980's:
- The
modular programming
technique
|
Characteristics:
-
Modular programming
decomposes
a large program
into
modules.
- A
module
can be one program file
or a small set of
source files
which
logically
groups the
related program code.
|
- Today's
programming technique for
large projects are
based on:
- The
object
concept
- An
object
represents
some
tangible item
used in the large
project
|
- The
style of programming using
objects is called:
-
Object Oriented Programming (OOP)
|
|
How
Object Oriented Programming
(OOP)
help you write
complex programs
-
Abstraction:
OOP provides
abstract classes
to help
reduce (= hide) details
-
Encapsulation:
prevents
code in other classes
from
accessing/modifying
important variables
to localize errors
-
Inheritance:
allows existing code to
be re-used
-
Polymorphism:
allows existing code to
be modified/enhanced
|
Object Oriented Programming
(OOP)
what is an object ?
- An object
represents
an
entity in the real world
that can be distinctly
identified.
- Examples of
objects:
- a student
(each student can be
uniquely identified)
- a circle
(each circle can be
uniquely identified)
- ...
|
- An
object has:
- A unique
identity
(you can tell different
objects apart)
- A
state
(discussed
next)
- A
behavior
(discussed
next)
|
- Object-oriented programming
(OOP):
|
The state
of objects
- The
state
of an object
consists of the
current properties of
the object
- The
state
of an object
(also known as its
properties
or
attributes)
is
represented by
data fields with their
current values.
Example:
- A
circle
object has
a radius = 5 and
color = "red"
- The
state of a
circle is
represented by
the
data fields
radius = 5
and
color = "red"
|
- A Java
class
is used group the
state/property
variables of
objects:
- The
instance variables
(that we will
study
later) inside
a
class
are used to
store
the state of
the object
- Each object will have
it's
own set of
instance variables
|
|
The
behavior
of objects
- The
behavior of an
object
are the
actions/operations that
an object can
perform
- The
behavior of an
object
are
implemented
by
methods.
- To
invoke a method
on an object
is to
"tell the object"
to perform an action.
Examples:
- A Circle
object can have the
getArea() and
getPerimeter()
methods
- invoking
getArea() will
instruct a
circle to
return its
area
- invoking
getPerimeter() will
instruct a
circle to
return its
perimeter
|
|
- A Java
class
is also
used to
group
methods that
operate on an
object:
- The
instance methods
(that we will
study
later)
are methods that
operate on
objects
-
All objects of
a class
share the
instance methods
(I.e.:
all objects of the
same
class have the
same
behavior)
|
|
Summary:
how to represent
real world
objects
using
classes in
Java
-
Summary:
a
Java class
defines the
(1)
properties and
(2)
behaviors for
objects:
- The
instance variables
in
a (Java) class
store the
current values of the
properties of
an object
- E.g.:
radius of
a circle object
Each
circle will have
its
own
radius
variable
|
- The
instance methods in a
(Java) class
defines the
behavior/actions of
the object
- E.g.:
getArea() of
a circle object
All
circles will
have the
same
getArea()
method
(returns
πr2)
|
|
- A
class
is
used
as a
template
(= description)
to
construct
the object's
data fields and
to define its
methods:
- When you
create
objects of
a class,
Java will
use the
class definition to
allocate the
instance variables
for that object.
- When you
invoke
some method on
an object,
Java will
run the
code in the
method definition
on the instance variables of
the object
|
|
Objects and
its class
- A class is a
blue print for
Java to
construct a
specific type
of object
- An object is an
instance
of some
class is
created using that
class'
blue print
- (Remember that the
class definition
describes the
properties
(= instance variables) and
behaviors
(= instance methods)
of these type of objects)
|
- You can create
any number
instances (= objects)
of a class
-
Each object will
have
its
own
properties
(= instance variables)
-
But:
all object will
share the
same
actions
(= instance methods)
|
-
Analogy to
explain:
class and
object:
- Class =
recipe to make
apple pie
- Object =
a
specific
apple pie
(e.g.:
John's
apply pie)
|
|
❮
❯