OBJECTS AND CLASSES
Martin Chapman
martin.chapman@kcl.ac.uk
•Fundamental principle:
•The difference between blueprints and
buildings
•Fundamental principle:
•The difference between blueprints and
buildings
data type
variable
variable
•Fundamental principle:
•The difference between blueprints and
buildings
int
num1 = 9
num2 = 20
•Fundamental principle:
•The difference between blueprints and
buildings
class
object
object
•There are two predominant ways to understand
Object Orientation:
•Understand how we do it.
•Understand why we do it.
How do we do it?
class
object
class
object
class
object
class
object
class
object
1. Make a class with methods and variables
2. Make an object (a variable) to link to that class
3. Reference the object to use the methods you
made in Step 1.
1. Make a class with methods and variables
2. Make an object (a variable) to link to that class
3. Reference the object to use the methods you
made in Step 1.
Among other reasons...
•It makes sense
•Code reuse
•State and behaviour
•It gives us control
•Constructors
•Encapsulation
Among other reasons...
•It makes sense
•Code reuse
•State and behaviour
•It gives us control
•Constructors
•Encapsulation
Dave
Victoria
Codereuse
Codereuse
Codereuse
davesCar victoriasCar
Codereuse
blue
davesCar victoriasCar
Codereuse
blue black
davesCar victoriasCar
Codereuse
Codereuse
blue black
davesCar victoriasCar
Codereuse
Among other reasons...
•It makes sense
•Code reuse
•State and behaviour
•It gives us control
•Constructors
•Encapsulation
Drive Colour
Speed
Number of wheels
Number
plate
Height
Stateandbehaviour
Paint
•All the information we want our program to have
about this object can be categorised as:
•“How the object is” (~Nouns / Adjectives).
•Colour, numberplate, speed and number of
wheels etc.
•“What can be done to the object” / “What the
object can do” (~Verbs).
•Drive and be painted
Stateandbehaviour
•All the information we want our program to have
about this object can be categorised as:
•“How the object is”.
•Colour, numberplate, speed and number of
wheels etc.
•“What can be done to the object” / “What the
object can do”.
•Drive and be painted
STATE
BEHAVIOUR
Stateandbehaviour
STATE
BEHAVIOUR
BEHAVIOUR
BEHAVIOUR
BEHAVIOUR
Stateandbehaviour
VARIABLES
METHOD
METHOD
METHOD
METHOD
Stateandbehaviour
Behaviour
Behaviour
Behaviour
Behaviour
State
FIELDS
METHOD
METHOD
METHOD
METHOD
Stateandbehaviour
StateVariables
(Properties)
Among other reasons...
•It makes sense
•Code reuse
•State and behaviour
•It gives us control
•Constructors
•Encapsulation
CONSTRUCTOR
A method which has the
same name as the class
is called an explicit
Constructors
Constructors
Constructors
Constructors
Among other reasons...
•It makes sense
•Code reuse
•State and behaviour
•It gives us control
•Constructors
•Encapsulation
Encapsulation
Private
Public
Only visible
inside
the class
Visible
anywhere
Encapsulation
Encapsulation
Encapsulation
Interface
Encapsulation
Encapsulation
• Object orientation, in its simplest form, is code within
code.
• By programming in an OO manner, we are designing code in a
way that both makes sense and gives us control.
• OOP makes sense because it allows for efficient reuse
and an accurate representation of the world using state
and behaviour.
• OOP gives us control through constructors and because it
enforces encapsulation (a black-box approach to
object design in which operations are performed through
well-defined method interfaces).
SUMMARY

Programming in Java: Object and Classes