Professional Documents
Culture Documents
L02 OOFundamental Part1
L02 OOFundamental Part1
Objectives
• Learning the “Art” of “Abstract Thinking”
• Review OO fundamental concepts
• class, object, state, method, method invocation
• inheritance, superclass, subclass, polymorphism
• sub-classing as subtyping
Aware of abstraction and encapsulation
class CalculatorApp
class Display
class Button
subclass SquareButton
class Calculator
subclass RectangleButton
7
Calculator
• Class Button
• Has attributes such as values and types
• Embedded functions
• Class Display
• Provides functions, displays calculations
• Class Calculator
• Contains class buttons (or different type of buttons, Square/Rectangle)
• Arrangements, coordination, such as an array of {Buttons}
• Class Calculator App
• Contains calculator object, which means you may have multiple
calculator instances, depends on your design.
• Main function
Objects and object classes (templates)
Template (class)
An object is an entity that has a state
and a set of operations on that state
A `software system’ entity that
represents instances of real-world and
system entities
State: a set of object attributes
Operations: provide services to other
objects (clients) which request these
services
An object class definition serves as a
template for creating objects
Declarations of the attributes and Mooncakes (objects)
services associated with an object of 8
Example Objects
• All Kings have something in common
An object
“instance”
Another object
“instance”
King Class
Let the set of all kings be called King Class
• Classes are templates for objects. They may be used to create objects.
• Multiple objects can be created from the same class.
Class
• A class is a set of related objects
Example: King Class presents a set of kings
• An object is an instance of a class
Example: King George III is an instance of King
Class, so is King Louis XVI…etc
• A class represents some useful concept that existing in
the problem/solution domains:
Concrete entities (existing in the real world):
Example: bank accounts, circles, and products
Abstract concepts: Example: windows
• Classes may inherit attributes and operations from
other classes
Another Example: Shoes
• A shoe has a
• Color (black, brown);
• Size (3, 8 ½ , 9½ , 14);
• Width (AAA, C, E);
• Style (stiletto heel, wingtip, moccasin, penny loafer);
• Material (leather, plastic)
• Color, size, width, style, and material all are attributes of
shoes
Shoe Class
• Shoe Class is the name of the set of all shoes
Object Book
- Describes the details of a book class Book
- Book Title, publisher, ISBN..etc
Object BookShelf
- Using some data structure (e.g. Array) class BookShelf
- Contains many Book objects
- i.e. [Book1][Book2][Book3]….[BookN]
Clock as a class:
Here are real objects of the Clock
Class Beyond the class name
Apart from the class name “Clock”, we need to tell others what
exactly is a clock in our system.
A class is a set (or template) of objects that
share the same class name (e.g., Clock),
17
18
Class Code
Class
Objects
Class structure
public class Clock
hour
hour and
and minutes
minutes are
are
{ the
the attributes
attributes of
of the
the
private int hour; class
class Clock
Clock
private int minute;
State of Object
“What is the time now?”
• It is a quarter to one.
• Now, it is __12:45_______
Methods of Classes
Two logical types of method
Operations
• Methods that change the value of some attribute of
an object (of its own or some other classes) e.g.
setName(String name)
Queries
• A query is a method that merely reports the value
of some attribute of an object or some
computation results without modifying the object’s
state. e.g. String getName()
24
1.
1. Do
Do you
you feel
feel comfortable
comfortable if
if 2.
2. Do
Do you
you feel
feel comfortable
comfortable if
if
getBalance()
getBalance() isis an
an operation
operation deposit()
deposit() is query that
is aa query that
that
that may
may change
change the
the value
value of
of does
does not
not increment
increment the
the
“balance”
“balance” of
of your
your account
account?? “balance”
“balance” of
of your
your account
account??
Messages Passing / Object Calling
l Object.Method (Object-dot-method)
l Objects communicate by message passing: request from one
object to another asking the second object to execute one of its
methods.
State of Object
• Objects can be in different states.
• Example: A kettle may be full or empty, on or off,
cold or hot, and the state will change under the
influence of outside circumstances (some event or
action happens).
State of Object
Suppose x is an integer, whose initial value is 10. : Direct
Direct effects
effects on
on
primitive data
initial value of x final value of primitive data
types
types such
such asas
x
integer,
integer, double,
double,
x = x – 10 10 0
string.
string.
x = x – 20 10 10
x = x – 30 10 20
extends extends
“Subclass/Child class”
29
Inheritance
X inherits from another class Y, if X is a
• A class
subclass of Y having additional (or modified)
attributes or methods MathematicalObject
• Y is called thesuperclass
X is called the subclass Shape Point Matrix
Rectangle
UML
UML
notation
notation
Inheritance
To Model “is kind of” relationship.
Example:
Staff – is kind of – Person
Student – is kind of – Person
iPhone – is kind of – Phone
Parent class
An abstract Super
class Class More general
Generalization
relationship
Sub More specific
Class
Sub-classes
A Library Class Hierarchy Example
Additional
attributes
34
Inheritance Example
In C++
class Clock {…};
class StopWatch: Clock {…};
Using Inheritance
• Inheritance (generalization) represent the is-a relationships
between classes of objects.
• For instance, a "fruit" is a generalization of "apple", "orange", "mango" and many others.
One can consider fruit to be an abstraction of apple, since apples are fruit (i.e., an apple is-
a fruit), apples may naturally inherit all the properties common to all fruit.
Private vs Public
Information Hiding in OO (1)
• Information hiding enhances maintainability because
implementation details are invisible outside an object
• Example: class BankAccount
• double - As a dollar amount with two decimal places for the cents