Object-Oriented Concepts: A Review
Object-Oriented Concepts: A Review
Concepts
A Review
Terminology
Object: A thing that exists in the domain of
the problem
E.g. An office building might have a number of
‘Elevators’, ‘Offices’, ‘SecurityDoors’, etc.
Software is object-oriented if the design and
implementation of that software is based on
the interaction between the objects of the
domain
Terminology
Class: A template used for the creation of
objects
A class describes various attributes an object
might have
e.g. A ‘Person’ class might have various
attributes, including age, weight, height, eye
colour, address, etc.
A Case Study
A Grocery Store
A Case Study: Grocery Store
A grocery store has a number of ‘classes’ and
‘objects’ in its domain:
Shopping carts, shelves, cashiers, and various items
A shopping cart is a class or template that describes
all shopping carts
Several instances of shopping carts, each with its own
distinct properties, may exist in the grocery store
These shopping cart instances are objects in the grocery
store
Grocery Store Classes
Carrier Item
Bread Fruit
Basket Cart
Apple Orange
Classes and Objects
Students often have trouble with the
difference between classes and objects
‘Apple’ in our example is a class
A grocery store may have hundreds of instances of
this class
Large apples, small apples, red apples, green apples,
etc.
Classes and Objects
Since ‘Apple’ is a class, and a class is a template, it
makes sense that you can’t eat ‘Apple’
However, you could eat ‘an apple’, which would be an
instance of an Apple
It should be obvious that, if you eat an apple, the template
‘Apple’ still exists, as well as other instances
If you eat an apple, other apples to not disappear, nor does
the existence of a fruit called an Apple
Classes and Objects
Thus, typically one interacts directly with
class instances (objects) and not with classes
themselves
In fact, in the object-oriented paradigm, any
behaviour is defined using object interactions
For object-oriented software, an application is a
collection of objects which interact to create
software functionality
Classes and Objects
Let’s examine a software example
A Menu in a graphical user interface is an
example of a class
An application may have many menus (File, Edit,
Help, …)
This the application may have several Menu
instances (objects)
Classes and Objects
The ‘Menu’ class may have several attributes:
A list of menu items
A background colour
A label (‘File’, ‘Edit’, ‘Help’)
‘Menu’ may also have several actions that are
possible:
show(): - make the menu visible
hide(): - make the menu invisible
addMenuItem(): - add a menu item to the list
setLabel(): - define the menu’s label
Why Classes?
Classes evolved through the concept of
encapsulation
Encapsulation means taking all related elements
and packaging them inside a single unit
In everyday terms, encapsulation means that
objects should be specified in such a way, that
they define their own behaviour and attributes
Encapsulation: Subroutines
Encapsulation was first applied to program
behaviour alone
Programmers noticed that they had to repeat
code that was similar or identical many times in a
single program
e.g. The code to print a line of text to the monitor, the
code to multiple two numbers, etc.
Encapsulation: Subroutines
Eventually, programmers began placing this
code into accessible areas of memory
This code would be called repeatedly
These reusable code units were called
subroutines
Although modern programmers call them
functions, procedures, or methods
Encapsulation: Objects
The first incarnations of objects were called ‘records’
They typically placed related information together into a
single data structure
In C/C++: structs
In Pascal: records
interface CashRegister
total : num
enterItem() Implementation of enterItem()
getTotal() Implementation of getTotal()
pay() Implementation of pay()
interface DetailedCashRegister
items : List<Item>
enterItem() Implementation of enterItem()
getTotal() Implementation of getTotal()
pay() Implementation of pay()
Implementation
Data
Classes
Let’s expand our definition of classes, to
specifically refer to classes in software
Classes define everything that is common to
all instances:
Definitions of member operations
The actual code is defined with the class
Declarations of data members
The data type and name of the data members is
defined with the class
Objects
Objects, on the other hand, contain
everything that is different with each instance
The values of the data members is an example
Even though the data members are defined in the
class definition, the actual memory allocated to store
that data is associated with each object, not the class
Objects
In addition, the member operations on a class must
be associated with a specific object, so they can
access member data for that specific object
There are two ways this can be done:
The member operations themselves can be defined on
each object
Thus, operations are duplicated for each instance
The operations, when called, must be told which object is
involved
For example, a reference or handle to the object could be
passed along with the input parameters
Exceptions to this Rule
Sometimes, items normally associated with an
object instance, are associated with the class itself
Operations that do not access member variables or
member operations
Variables that should contain the same value for all
instances
When the value is changed using one instance, the value
on the other instances should also change
In Java and C++, such members are called ‘static’
variables and ‘static’ operations
In other languages, they are called class variables and
class operations
Messages
Messages are how objects interact
Messages typically involve an object making an
invocation of another object’s operation
Messages contain the following information:
A handle (or reference) of the object whose operation is to
be called
The name of the operation
A number of arguments to be passed to/from the operation
This could include input parameters, output parameters, or
parameters that are used for input and output
Inheritance
In the previous lecture, we saw a diagram illustrating
relationships between classes
One relationship classes can have is inheritance
A class A inherits from another class, B, if it
represents something more specific, but A is still a
type of B
e.g. Apple is a type of Fruit
e.g. Notebook is a type of Computer
e.g. JPEGImage is a type of Image
Inheritance
If class A inherits from class B
A is a subclass of B
B is a superclass of A
Questions:
Is it possible for a class to have more than one
subclass?
Is it possible for a class to have more than one
superclass?
Multiple Inheritance
Multiple inheritance makes sense in the real world:
Here is an example:
Susan works at a bank, thus she is an instance of the type
‘Banker’
Susan plays professional soccer, thus she is an instance of
the type ‘SoccerPlayer’
Possibly, SoccerPlayer is a subclass of a class called
Athlete or something similar
Therefore, Susan must be an instance of some class, that
is a subclass (or descendant) of SoccerPlayer and a
subclass (or descendant) of Banker
Multiple Inheritance
Java does not support pure multiple
inheritance
A Java class can inherit from only (and exactly)
one class
If none is specified, Object is assumed
A Java class can, however, implement as many
interfaces as you want
Multiple Inheritance
C++ classes can inherit from multiple classes
Here is an example:
Printer printer = …;
printer.print(Document doc);
Which version (on which subclass) of the ‘print’
method will be called?
It depends on the type of object stored in ‘printer’
Polymorphism
Here’s another example:
There is a class called ‘Car’ which defines a
method called ‘accelerate’
One subclass of ‘Car’ called ‘AutomaticCar’ would
define ‘accelerate’ in one way, while another
subclass ‘ManualCar’ would define it another way
Dynamic vs. Static Binding
In the example, we learned that sometimes a
runtime environment must determine which
version of an operation to execute at run time
This is known as dynamic binding
Normally, the operation to be executed is
determined at compile time
This is known as static binding
Overriding
Overriding is when a subclass defines an
operation that is already defined on the
superclass
Using dynamic binding, the subclass’ version of
the operation will be executed when a variable
contains an instance of the subclass
Overloading
Overloading is a similar concept to overriding
An operation (or operator, such as +, =, …) can be
defined more than once to operate on different
arguments
Thus a given operation name may be reused for several
operations, as long as the signature is different
An operation’s signature or template is its name, as well as
the number, order, and types of its arguments
Genericity
Genericity is the term applied to situations where
code operates on data, whose type is unknown until
run time
An example might be creating a quicksort operation
which takes an array
If this operation supports genericity, the type of the objects
in the array could be unknown until the program is actually
run
Now, the same operation can be used to sort integers,
strings, etc. by simply associating the operation with the
appropriate type within the program
Summary
So far, I have introduced the following vital ingredients in
object-oriented development:
Encapsulation
Classes
Objects
Information hiding
Implementation hiding and interfaces
Inheritance
Messages
Polymorphism
Overriding
Overloading
Genericity
Summary
To give you a foundation, I’ll now give you
examples of each in some OOP languages
I will give Java examples
In some cases, Java5 will be used when a feature
is not available in previous versions
Encapsulation
Java, C++, and Eiffel use ‘classes’ for encapsulation
In all 3 languages, operations and attributes can be
combined into a single unit
In Java:
obj.doSomething(11);
or
person.birthday();
Polymorphism in Java
Dynamic binding is automatically used in Java, so polymorphism
is a natural consequence:
public class A {
private int a = 0;
void doSomething(int val) { a = val; }
}
public class B extends A {
void doSomething(int val) { a = val * val; }
}
Overriding in Java
Dynamic binding would be used for:
A obj = null;
obj = new A();
obj.doSomething(15);
obj = new B();
obj.doSomething(15);
Overloading in Java
See the following code in Java:
public class Printer {
void print(Document doc) { … }
void print(Image image) { … }
}
The operation ‘print’ has been overloaded to work
with different attribute sets
As you might imagine, the implementations of each version
of ‘print’ could be totally different
Overloading and overriding is essentially identical in
both languages
Although some versions of C++ do not support them
Genericity
Genericity is possible in Java5
Consider this example: