CHAPTER 11
SUPPORT FOR OBJECT-ORIENTED PROGRAMMING
INTRO
- The concept of object-oriented programming has
its roots in Simula 67 but was not fully developed until the evolution of
Smalltalk.
- A language that is
object-oriented must provide support for 3 key language features: abstract
data types, inheritance, and a special type of dynamic binding.
- Procedure-oriented
programming was popular in the 70’s, it focuses on subprograms and
subprogram libraries.
- Data-oriented
programming focuses on abstract data types.
- Languages that support
data-oriented programming are often called object-based languages.
INHERITANCE
- By the middle of the
80’s, it was apparent to many software developers that one of the best
opportunities to increase productivity in their profession was in software
reuse. Abstract data types, with
their encapsulation and access controls, were the units to be reused.
- Inheritance offers a
solution to both the modification problem posed by data type reuse and the
program organization problem.
- Class instances are
called objects
- A class that is defined
through inheritance from another class is a derived class or subclass
- A class form which the
new class is derived is its parent
class or superclass
- The subprograms that
define the operations on objects of a class are called methods.
- The calls to methods
are often called messages
- The entire collection
of methods of an object is called the message
protocol of the object.
- Some object-oriented
languages include a third category of access control, often called protected, that is used to provide
access to derived classes while withholding it from other classes
- A modified method has
the name, and often the same protocol as the one of which it is a
modification. The new method is
said to override the inherited
version, which is then called the overridden
method.
- Classes can have 2
kinds of methods and 2 kinds of variables. The most used methods and variables are called instance methods and variables.
- Class variables belong to the class
- Class methods can perform operations on the class
- If a class created by
inheritance has a single parent class, then the process is called single inheritance. If a class has more than one parent
class, the process is called multiple
inheritance.
- One disadvantage of
inheritance as a means of increasing the possibility of reuse is that it
created a dependency among the classes in an inheritance hierarchy.
POLYMORPHISM
AND DYNAMIC BINDING
- A characteristic of
object-oriented programming languages is a kind of polymorphism provided
by the dynamic binding of messages to method definitions. This is supported by allowing one to
define polymorphic variables of the type of the parent class that are also
able to reference objects of any of the subclasses of that class.
- Dynamic binding through
polymorphic variables is a powerful concept
- The abstract method is often called a
virtual method
- Any class that includes at least one
virtual method is called a virtual
class.
COMPUTING
WITH AN OBJECT-ORIENTED LANGUAGE
- All computing in a pure
object-oriented language is done by the same uniform technique: sending a
message to an object to invoke one of its methods.
- A reply to the message
is an object that returns the value of the computation of the method.
DESIGN ISSUES FOR OBJECT-ORIENTED LANGUAGES
- In the purest model of
object-oriented computation, all types are classes. There is no distinction between
predefined and user-defined classes.
In fact, all classes are treated the same way and all computation
is accomplished through message passing.
- A derived class is a subtype if it has an is-a
relationship with its parent.
- There are 2 kinds of
type checking that must be done between a message and a method in a
strongly typed language: the message’s parameter types must be checked
against the method’s formal parameters, and the return type of the method
must be checked against the message’s expected type.
- There are 2 design
questions concerning the allocation and deallocation of objects. The first
is the place from which objects are allocated. If they behave like the abstract data types, then they can
be allocated from anywhere.
- The second question
here is concerned with those cases where objects are allocated from the
heap. The question is whether
deallocation is implicit or explicit or both.
- Dynamic binding of
messages to methods in an inheritance hierarchy is an essential part of
object-oriented programming. The
question here is whether all binding of messages to methods is dynamic.
OVERVIEW OF SMALLTALK
- A program in Smalltalk
consists entirely of objects.
- All objects are treated
uniformly. They all have local
memory, inherent processing ability, the capability to communicate with
other objects, and the possibility of inheriting methods and instance
variables from ancestors.
- All Smalltalk objects
are allocated from the heap and are referenced through reference variables,
which are implicitly de-referenced.
- The Smalltalk system integrates
a program editor, compiler, the usual features of an operating system, and
a virtual machine into a single system.
- Smalltalk methods are
constructed from expressions. An expression
specifies an object, which happens to be the value of the expression.
- The most common
literals are numbers, strings and keywords.
- Smalltalk variables
come in two varieties: private,
which means they are local to an object, and shared, which means they are
visible outside the object in which they are declared
- All Smalltalk variables
are references; they can only refer to objects or classes.
- Instance variables are
either named or indexed.
- Messages have the form
of expressions. They provide the
means of communicating among objects and are the way operations of an
object are requested.
METHODS
- The general syntactic
form of a Smalltalk method is
- Message_pattern [ |
temporary variables | ] statements
- where the brackets are metasymbols
that indicate that what they enclose is optional.
- Because Smalltalk has
no type declarations, temporary variables, when present, need only be
named in a list.
- Any message expression,
literal object, or variable name can be the right side of an assignment
statement. The left side is a
variable name, and the operator is specified with a left arrow, as in
- total <- 22.
- Sum <- total
BLOCKS
- A block is an unnamed
literal object that contains a sequence of expressions. Blocks are instances of the class
Block.
- A block is specified in
brackets, with its expression components separated by periods,
- [index <- index + 1. Sum <-
sum + index]
- blocks are executed in
the context of their definition, even when they are sent as parameters to
a different object.
CLASSES
- A Smalltalk class has 4
parts:
- A class name
- The superclass name
- Declarations of the
- Declarations of the
instance and class methods
- Instance variables are
not visible to other objects. Each
instance variable refers to on object, called its value.
TYPE
CHECKING AND POLYMORPHISM
- The dynamic binding of
messages to methods in Smalltalk operates as follows: a message to an object causes the class
to which the object belongs to be searched for a corresponding
method. If the search fails, it is
continued in the superclass of that class.
- The only type checking
in Smalltalk is dynamic.
INHERITANCE
- A Smalltalk subclass
inherits all of the instance variables, instance methods, and class
methods of its superclass. The subclass
can also have its own instance variables.
- The subclass can define
new methods and redefine methods that already exist in an ancestor class.
***
Read about C++ from p.466 to p.474
***Read
about Java from p.474 to p.476
***Read
about Ada from p.476 to p.480
-