Object Orientation - University of Scranton: Computing Sciences Dept.

Download Report

Transcript Object Orientation - University of Scranton: Computing Sciences Dept.

Object Orientation
Yaodong Bi, Ph.D.
Department of Computer Sciences
University of Scranton
April 8, 2016
Classes and objects for modeling

Classes
– Real world concepts
– Student, course, bicycle, etc

Objects
– Instances of real word concepts
– John Smith, Operating Systems,
Mongoose Mountain S100, etc
4/8/2016
2
Classes and objects for modeling
Classes
4/8/2016
“Customers
John
and
Susan
entered the
MakeMoney
bank
and were served by
teller
Andy.”
Objects:
3
Classes and objects in software

Objects
– Packaging both data and the procedures that operate on
the data into one
– Operations are the only way to change the data items –
encapsulation

Classes
– Template of objects of the same type
– Write once and use many times

Implementation classes/objects
– Some classes/objects do not match to concepts in the real
world


4/8/2016
List, stack, tree, queue – containers
Exception classes
4
Attributes and operations

Instance attributes (simply attributes)
– Properties (internal state) of an object


Student: name, major, class, GPA
Course: title, # of credits, description
– Each object has its own value for an attribute

Instance operations (simply operations)
– Services of an object

Stack: push, pop, isEmpty, isFull
– Encapsulation


4/8/2016
Attributes (private and protected) are not accessible
directly by client
Operations are used to read/write the values of
attributes
5
Attributes and Operations

Class attributes (static attributes)
– Represent properties that can be applied to all
objects of the same class
– Only one value for each class attribute shared by
all objects of the class

All accounts of the CheckingAccount class have the
same interest rate
– Can be accessed in both instance and class
operations

Class operations (static operations)
– Used to access class attributes
– Cannot access instance attributes and operations
4/8/2016
6
Class member access modes

Public
– Public attributes and public operations
– Accessible to all clients or the world

Protected
– Protected attributes and operations
– Accessible to the class and its subclasses

Private
– Private attributes and operations
– Only accessible by the operations of the class
4/8/2016
7
Member access modes in Java
Specifier
Class
Package Subclass World
Private
Y
N
N
N
No Specifier
Y
Y
N
N
Protected
Y
Y
Y
N
Public
Y
Y
Y
Y
4/8/2016
8
Abstract and concrete classes

Abstract classes
– Define a common interface for its subclasses
– Defer some or all of its implementation of
operations to its subclasses
– Cannot be instantiated

Concrete classes
– That are not abstract classes
– Can be instantiated
– Concrete subclasses implement all the
operations
4/8/2016
9
Interfaces

Signature of an operation:
– Operation name
– Objects/classes it takes as parameters
– Return value and type

Interface:
– The set of signatures of related operations
– Representing a capability or a set of services
– A class may implement multiple interfaces

4/8/2016
Its objects have more than one set of services
10
Inheritance





Specifies is-a or a-kind-of relationship
Generalization and specialization
Superclasses and subclasses
Single and multiple inheritance
Class and interface inheritance
4/8/2016
11
Superclasses and subclasses

Superclass
– Also called base class or parent class
– All of its members are inherited by its
subclasses

Subclasses
– Also called child classes
– Inherit all the properties of its
superclasses
4/8/2016
12
Single and multiple inheritance

Single inheritance
– A subclass cannot have more than one
parent

Multiple inheritance
– A subclass may have more than one
parent
– Java does not allow multiple inheritance
– Java allows multiple interface inheritance

4/8/2016
Through interface implementation
13
Class inheritance lattice
Person
SSN
Name
getSSN()
getName()
Faculty
office
getOffice()
Student
major
getMajor()
Staff
dept
getDept()
FullTime
salary
getSalary()
Graduate
PartTime
PayRate
getPayRate()
PayRate
getPayRate()
Underg
PayRate
getPayRate()
TA
4/8/2016
labs
getLabs()
14
Implementation inheritance




Also called class inheritance
Define an object’s implementation in
terms of another object’s
implementation
Pure class inheritance in C++
Pure class inheritance in Java
4/8/2016
15
Pure class inheritance with C++
Class BinaryTree {
getRoot() {;}
setRoot() {;}
leftTree() {;}
rightTree() {;}
}
The operations of BinaryTree
are not accessible/visible to the
clients of BinSearchTree
because BinaryTree is private
4/8/2016
Class BinSearchTree:
private BinaryTree
{
insert()
{;}
remove() {;}
find() {;}
}
They use the operations of
BinaryTree
16
Pure class inheritance with Java



Java does not allow private parent
The parent and grandparents are
public and accessible through the
subclass
Java cannot implement pure
implementation inheritance.
4/8/2016
17
Interface inheritance


Describe when an object can be used
in place of another
Pure interface inheritance in C++
– Superclasses are pure abstract classes

Pure interface inheritance in Java
– Superclasses are Java interfaces
4/8/2016
18
Interface inheritance with C++
Class Stack {
virtual push() = 0;
virtual pop() = 0;
virtual isEmpty() = 0;
virtual isFull() = 0;
}
A C++ Abstract class
Class MyStack: public Stack
{
push()
{;}
pop() {;}
isEmpty() {;}
isFull() {;}
}
4/8/2016
Class YourStack: public Stack
{
push()
{;}
pop() {;}
isEmpty() {;}
isFull() {;}
}
19
Interface inheritance with Java
Interface Stack {
push();
pop();
isEmpty();
isFull() 0;
}
A Java Interface
Class MyStack
implements Stack {
push()
{;}
pop() {;}
isEmpty() {;}
isFull() {;}
}
4/8/2016
Class YourStack
implements Stack {
push()
{;}
pop() {;}
isEmpty() {;}
isFull() {;}
}
20
Polymorphism



When a client sends a request to a
reference, the method executed depends on
the object behind the reference
Polymorphism and inheritance are very
powerful tool in software design
So powerful that if they are used properly,
they can hurt really bad
– If something goes wrong, it is hard to tell which
class/object caused the problem
4/8/2016
21
Dynamic (late) binding




Dynamic binding is a way of implementing
polymorphism
It means that a function call is not linked to the
actual function until execution time
Since a reference may be used to point to different
types (subtypes or subclasses) of objects at
execution time, so the actual function to be
executed may not be known when the program is
compiled.
The opposite of dynamic binding is static binding
which links a function call to the actual function at
compilation time.
4/8/2016
22
Method Overriding



A subclass overrides the operation(s)
defined in the parent/grandparent classes.
This is for specialized subclasses to override
the behavior defined in the super class(es).
For example:
– Superclass Employee’s print method prints
employee’s SSN, Name, Address.
– Its subclass Engineer’s print method prints
engineer’s type and level in addition to
employee’s normal information.
4/8/2016
23
Function overloading


The same name is used for more than one
function
For example, a function of adding two
integers may be named the same as the
one of adding two floats:
– Add(int x, int y);
– Add(float x, float y);
– Two functions use the same name for the same
purpose with different data parameters
4/8/2016
24
Inheritance vs. Composition


Two common techniques for reuse
Class Inheritance
– White-box reuse
– Defined at compile-time, statically
– Cannot change parent at rum-time

Object Composition
– Black-box reuse
– A reference/pointer to the object
– Can be changed at run-time

Favor composition over inheritance
4/8/2016
25
Delegation


A way to make composition as powerful as
class inheritance
A receiving object delegates requests
to its delegates
== subclass defers request to parent class

In class inheritance
– parent can use this to access the child

In delegation
– Receiving object can pass itself to its delegate
4/8/2016
26
Three techniques for reuse

Class Inheritance
– can provide default operations and let subclass override
them
– Cannot be changed at run-time

Object Composition
– Can change the behavior being composed at run-time
– Requires redirection and so becomes less efficient

Generics
– Changes the types that a class can use
– Cannot change at run-time
4/8/2016
27
Toolkits


A set of predefined classes
Provides useful, general-purpose
functionality
– Collection classes (list, stack, etc)
– GWT (Google Web Toolkit)


Does NOT impose any design level
decision
Code Reuse
4/8/2016
28
Frameworks


A reusable design for a type of applications
Dictates the architecture of your app
– Overall structure
– Key components and their responsibilities
– How classes/objects interact with each other


Design reuse
Inversion of Control
– Dependency Injection
4/8/2016
29
Frameworks

Framework examples:
– Web Apps

J2EE, Spring Framework, .Net
– ORM (object to relation mapping)

Hibernate, OpenJPA, MyBatis
– Mobile Apps
Android
 iOS

4/8/2016
30