What is an Object? (1)

Download Report

Transcript What is an Object? (1)

CS565 Advanced Software Development
Lecture 2: Object Oriented Concepts
A. O’Riordan, 2006
What is an Object? (1)




fundamental unit of abstraction in the object paradigm is the
object
that which represents an individual, identifiable item, unit, or
entity, either real or abstract, with a well-defined role in the
problem domain [Smith and Tockey]
anything with a crisply defined boundary [Cox91]
an abstraction of a set of real-world things such that: all of the
real-world things in the set - the instances - have the same
characteristics - all instances are subject to and conform to the
same rules [Shlaer88]
7/20/2015
The OO Paradigm, A.O'Riordan
What is an Object? (2)

an object has state, behaviour, and identity; the structure and
behaviour of similar objects are defined in their common class;
the terms instance and object are interchangeable [Booch94]

objects are made up of both data and operations

all interactions between objects are via their operations
7/20/2015
The OO Paradigm, A.O'Riordan
Object Encapsulation



each component of the system should encapsulate or hide a
single design decision
Information Hiding: process of hiding all the details of an object
that do not contribute to its essential characteristics
Java/C++/C# have a very general encapsulation/protection
mechanism with public, private and protected members

Public members (member data and member functions) may be
accessed from anywhere. Private members are only accessible from
within a class. Protected members are accessible from within a
class and also from within subclasses
7/20/2015
The OO Paradigm, A.O'Riordan
Messages



A single object alone is generally not very useful. Instead, an
object usually appears as a part of a larger program or
application that contains many other objects. Through the
interaction of these objects, programmers achieve higher-order
functionality and more complex behaviour.
Software objects interact and communicate with each other by
sending messages to each other. When object A wants object B
to perform one of B's methods, object A sends a message to
object B.
Sometimes, the receiving object needs more information so that
it knows exactly what to do; This information is passed along
with the message as parameters.
7/20/2015
The OO Paradigm, A.O'Riordan
Design By Contract

the interface is often understood as defining a contract between
the clients of an object and the implementer of that object



can implement in any way that he/she wishes
as long as the interface itself remains unchanged
if interface is changed then the contract has to be broken and
subsequently renegotiated
7/20/2015
The OO Paradigm, A.O'Riordan
Classes




a set of objects that share a common structure and a common
behaviour
a single object is simply an instance of a class
for example, in Java/C++, a class is first declared and a object
of that class can then be created having the class as its type (2level type system)
some programming languages make no distinction between
objects and classes (1-level type system)
7/20/2015
The OO Paradigm, A.O'Riordan
Class Variables and Methods



In addition to instance variables, classes can define class
variables. A class variable contains information that is shared by
all instances of the class.
All instances share this variable. If one object changes the
variable, it changes for all other objects of that type.
A class can also declare class methods. You can invoke a class
method directly from the class, whereas you must invoke
instance methods on a particular instance.
7/20/2015
The OO Paradigm, A.O'Riordan
Classification - Inheritance (1)

is common when classifying things in the real world to order
them into hierarchical structures



taxonomy that zoologists have developed for the animal kingdom
animals are named and categorised based on the common and
distinguishing physical and behavioural traits
inheritance is sometimes called programming-by-extension
because new functionality may be added by enhancing that
which exists
7/20/2015
The OO Paradigm, A.O'Riordan
Inheritance (2)

sometimes called generalisation/specialisation


language features for representing inheritance


other terminology is also used for this relationship such as
parent/child class and base/derived class
C++, Java, Smalltalk, Eiffel, etc. have built-in inheritance features
delegation is a technique that is employed in some languages
instead of inheritance.

each object has a delegate list instead of a parent. An object
delegates responsibility for any message it can’t handle to its
delegate
7/20/2015
The OO Paradigm, A.O'Riordan
Inheritance (3)

Designing inheritance hierarchy is a difficult craft


different views of classification as useful in OO analysis and design:
e.g. classical categorization (look for common properties in
objects), and conceptual clustering (conceptual descriptions of
groups of objects)
member look-up


an invoked operation could reside at any level in the inheritance
tree, it may be in the local object, or in any of the ancestral objects
up to the base class object
Java/C++ finds the closest match within the scope
7/20/2015
The OO Paradigm, A.O'Riordan
Multiple Inheritance

where a subclass inherits from more than one superclass


since several distinct parents can declare a member within a
multiple inheritance hierarchy, which one to choose then becomes
an issue
implementations




C++ declares an error if and only if a conflict arises, but a class
qualifier can be used to explicitly disambiguate
Smalltalk renders same names for instance variables of subclasses
illegal.
Eiffel forces derived classes to rename parent members that
conflict
Self prioritizes parents
7/20/2015
The OO Paradigm, A.O'Riordan
Polymorphism




many forms
the ability of an object (or reference) to assume (be replaced
by) or become many different forms of object. An object (or
reference) can assume or become another object (possibly
satisfying some implicit or explicit type constraints or a common
structure)
in many programming languages, e.g. C++, polymorphism is
closely tied to inheritance
contrast a language with no support for polymorphism is said to
be monomorphic
7/20/2015
The OO Paradigm, A.O'Riordan
Typing



in statically typed languages, the types are declared in a
program prior to compilation
dynamic typing uses the inherent types of polymorphic objects,
keeping track of the types of objects at run-time
many languages provide dynamic typing: Smalltalk, Self,
Objective-C, etc. A limited dynamic typing scheme RTTI is part
of the ISO C++ standard.
7/20/2015
The OO Paradigm, A.O'Riordan
Abstract Classes and Generics
Abstract Classes
classes may be defined to be abstract - meaning that they
cannot be instantiated - most OO programming languages
support abstract classes
Generics
called templates in C++ - an example is a Stack with a
generically parameterized base type. This allows a single Stack
class to provide many instantiations such as a Stack of ints, a
Stack of any fundamental or user defined type
7/20/2015
The OO Paradigm, A.O'Riordan
History (1)



beginnings in hardware over twenty years ago with descriptorbased and capability-based architectures, which came about
through attempts to close the gap between the high-level
abstractions of programming languages and the low-level
abstractions of the machine itself
objects first appeared in Simula-67, a simulation programming
language
Smalltalk took the OO paradigm to further by making everything
in the language and environment an object
7/20/2015
The OO Paradigm, A.O'Riordan
History (2)

OO versions of existing programming languages soon began to
appear





C++ and Objective C are OO versions of C
Adding OO programming to Pascal led to Ada, Object Pascal and
Oberon
Many dialects of Lisp incorporated OO features including Common
Lisp Object System (CLOS)
software engineering ideas such as the entity-relationship (ER)
approach to data modelling
in artificial intelligence, developments in knowledge
representation have contributed to OO abstraction
7/20/2015
The OO Paradigm, A.O'Riordan
Object Implementation


objects can be viewed as instances of a record type, whose
record fields are called instance variables (Smalltalk) or member
data (C++). The record can also of course contain operations,
which are called methods (Smalltalk) or member functions
(C++).
can be viewed as functions taking the object of the record type
as a parameter

the implicit object is called the receiver - called self (Java/Smalltalk)
or this (C++)
7/20/2015
The OO Paradigm, A.O'Riordan
Object Oriented Programming
Some common object oriented programming languages are:

C++ (based on C)

Java

C#

Python
 Smalltalk

CLOS

Eiffel

Simula

Oberon

Objective C
7/20/2015
The OO Paradigm, A.O'Riordan
Example: C++




C++ is based on C and is a superset of C.
C++ was developed by Bjarne Stroustrup at Bell Labs in the
early 1980s.
C++ retains C's power and flexibility to deal with the hardwaresoftware interface and low-level systems programming. The
efficiency, economy, and power of expression of C are retained
in C++.
C++ is a hybrid language. In practice, C++ software reflects
both the procedural programming paradigm as well as objectoriented paradigm. This presents a special challenge to the
beginning C++ programmer.
7/20/2015
The OO Paradigm, A.O'Riordan
Advantages of the object paradigm (1)

reuse of code

design reuse (e.g. design patterns, frameworks)



well established in the software
programming language community
engineering
and
standardisation, e.g. the UML is supported by the OMG, and
C++ is a full ISO standard
systems are more resilient to change
7/20/2015
The OO Paradigm, A.O'Riordan
Advantages of the object paradigm (2)



reduce the conceptual gap between the different phases of
the development lifecycle since essentially the same
conceptual model/terminology is employed throughout the
lifecycle
reduces development risks because fits with risk-reducing
technology such as prototyping and COTS components
appeals to human cognition, ideas such as class and
inheritance are familiar terms from everyday life
7/20/2015
The OO Paradigm, A.O'Riordan