Objects and Classes
Object-Oriented Programming
Outline
■ Abstract Data Types and Classes
■ Sample programs
■ OOP concept: Abstraction
■ OOP concept: Encapsulation
Readings:
■ IPIJ: Ch.3.
Introduction to Java 2
Abstract data types
A data type is a set of values and a set of operations
on those values
Primitive types
■ values immediately map to machine representations
■ operations immediately map to machine instructions.
We want to write programs that process other types of data.
■ Colors, pictures, strings,
■ Complex numbers, vectors, matrices, graphs…
Objects and Classes 3
Object-oriented programming (OOP)
Class == Abstract Data Type
Object == Variables
OOP: create classes and manipulate the objects in your program
(i.e. create your own data types and use them in your program)
Clients can use ADTs
without knowing implementation details.
Our tasks:
■ How to create classes?
■ How to use classes and objects?
Objects and Classes 4
Using a class
How to construct new objects
■ Use the keyword new to invoke a constructor.
■ Use data type name to specify type of object.
How to apply operations to a given object
■ Use object name to specify which object.
■ Use the dot operator to indicate that an operation is to be
applied.
■ Use a method name to specify which operation
Objects and Classes 5
Color ADT
6
Using color: monochrome luminance
7
Computing with color: grayscale
8
Picture ADT
9
Picture client example: Grayscale filter
10
Picture client example: Grayscale filter
11
OOP Concept: Abstraction
■ Abstraction: to distill a complicated system down to
its most fundamental parts and describe these parts
in a simple, precise language.
❑ naming the parts
❑ explaining their functionality
12
OOP Concept: Abstraction
■ Examples Design of data 🡪 abstract data types
13
Classes
■ Classes
❑ are the templates to create objects (instantiate).
❑ Each object has the same structure and behaviour as the
class from which it was created
■ “Data type – Variable” relation
❑ Classes are what we design and code. Class definitions
make up programs.
❑ Objects are what are created (from a class) at run-time
OOP Concepts 14
Objects
■ Object Identities
❑ Java object ids, probably the address of the object
■ Object State 🡪 Attributes / Instant variables
❑ (20lit, 0km/h,”143 WJT”)
■ Object Behavior 🡪 Methods
❑ Operations/services performed on the object.
❑ accelerate()...
OOP Concepts 15
class Car {
Encapsulation / public void setSpeed(…)
{
// check validity
Information hiding // set new values
...
}
■ Encapsulation: to group related things together, ...
so as to use one name to refer to the private double speed;
whole group. }
❑ Functions/procedures encapsulate instructions
❑ Objects encapsulate data and related procedures
■ Information hiding: encapsulate to hide
internal implementation details from outsiders
❑ Outsiders see only interfaces
❑ Programmers have the freedom in implementing
the details of a system.
❑ Hence, the ability to make changes to
an object’s implementation without affecting
other parts of the program
OOP Concepts 16
Inheritance
Mom’s eyes
Dad’s smile
Mom’s love
Dad’s sports
of ROCK
obsession
OOP Concepts 17
Inheritance
■ “is-a” relations
■ The general classes can be
specialized to
more specific classes
■ Reuse of interfaces & implementation
■ Mechanism to allow derived classes to possess attributes
and operations of base class, as if they were defined at
the derived class
■ We can design generic services before specialising them
OOP Concepts 18
Polymophism Jump!
Polymorphism:
❑ "more than one form"
Object polymorphism:
■ Different types of objects can respond to the same message
■ The actual method that executes is not determined until
run-time
❑ Dynamic (or late) binding
■ Example: on receiving message rotate(), the square and the
amoeba respond by doing different things.
OOP Concepts 19