CH 12
CH 12
CH 12
ISBN 0-321-19362-8
Chapter 12 Topics
• Introduction
• Object-Oriented Programming
• Design Issues for Object-Oriented Languages
• Support for Object-Oriented Programming in Smalltalk
• Support for Object-Oriented Programming in C++
• Support for Object-Oriented Programming in Java
• Support for Object-Oriented Programming in C#
• Support for Object-Oriented Programming in Ada 95
• The Object Model of JavaScript
• Implementation of Object-Oriented Constructs
• Paradigm Evolution
1. Procedural - 1950s-1970s (procedural abstraction)
2. Data-Oriented - early 1980s (data abstraction)
3. OOP - late 1980s (inheritance and dynamic
binding)
• Origins of Inheritance
– Observations of the mid-late 1980s :
• Productivity increases can come from reuse
• Unfortunately,
– ADTs are difficult to reuse--never quite right
– All ADTs are independent and at the same level
• Inheritance solves both--reuse ADTs after
minor changes and define classes in a
hierarchy
• OOP Definitions:
– ADTs are called classes
– Class instances are called objects
– A class that inherits is a derived class or a subclass
– The class from which another class inherits is a
parent class or superclass
– Subprograms that define operations on objects are
called methods
• Polymorphism in OOPLs
– A polymorphic variable can be defined in a class
that is able to reference (or point to) objects of the
class and objects of any of its descendants
– When a class hierarchy includes classes that
override methods and such methods are called
through a polymorphic variable, the binding to the
correct method MUST be dynamic
– This polymorphism simplifies the addition of new
methods
• Inheritance
– All subclasses are subtypes (nothing can be
hidden)
– All inheritance is implementation inheritance
– No multiple inheritance
– Methods can be redefined, but the two are not
related
• Evaluation of Smalltalk
– The syntax of the language is simple and regular
– Good example of power provided by a small
language
– Slow compared with conventional compiled
imperative languages
– Dynamic binding allows type errors to go
undetected until run time
– Greatest impact: advancement of OOP
• General Characteristics:
– Mixed typing system
– Constructors and destructors
– Elaborate access controls to class entities
• Inheritance
– A class need not be the subclass of any class
– Access controls for members are
1. Private (visible only in the class and friends)
(disallows subclasses from being subtypes)
2. Public (visible in subclasses and clients)
3. Protected (visible in the class and in subclasses,
but not clients)
• Inheritance (continued)
– In addition, the subclassing process can be
declared with access controls (private or public),
which define potential changes in access by
subclasses
a. Private derivation - inherited public and protected
members are private in the subclasses
b. Public derivation public and protected members
are also public and protected in subclasses
• Reexportation
– A member that is not accessible in a subclass
(because of private derivation) can be declared to
be visible there using the scope resolution operator
(::), e.g.,
class subclass_3 : private base_class {
base_class :: c;
…
}
• Reexportation (continued)
– One motivation for using private derivation:
• A class provides members that must be
visible, so they are defined to be public
members; a derived class adds some new
members, but does not want its clients to see
the members of the parent class, even though
they had to be public in the parent class
definition
• Dynamic Binding
– A method can be defined to be virtual, which
means that they can be called through
polymorphic variables and dynamically bound to
messages
– A pure virtual function has no definition at all
– A class that has at least one pure virtual function is
an abstract class
• Evaluation
– C++ provides extensive access control (unlike
Smalltalk)
– C++ provides multiple inheritance
– In C++, the programmer must decide at design
time which methods will be statically bound and
which must be dynamically bound
• Static binding is faster!
– Smalltalk type checking is dynamic (flexible, but
somewhat unsafe)
– Because of interpretation and dynamic binding,
Smalltalk is ~10 times slower than C++
• Inheritance
– Single inheritance only, but there is an abstract
class category that provides some of the benefits
of multiple inheritance (interface)
– An interface can include only method declarations
and named constants, e.g.,
public class Clock extends Applet
implements Runnable
– Methods can be final (cannot be overriden)
• Dynamic Binding
– In Java, all messages are dynamically bound to
methods, unless the method is final (means it
cannot be overriden; therefore, dynamic binding
serves no purpose)
• Encapsulation
– Two constructs, classes and packages
– Packages provide a container for classes that are
related (can be named or unamed)
– Entities defined without a scope (access) modifier
have package scope, which makes them visible
throughout the package in which they are defined -
they go in the unnamed package
• Every class in a package is a friend to the package
scope entities elsewhere in the package
• So, package scope is an alternative to the friends of
C++
• General characteristics
– Support for OOP similar to Java
– Includes both classes and structs
– Classes are similar to Java’s classes
– Structs are less powerful stack-dynamic constructs
• Inheritance
– Uses the syntax of C++ for defining classes
– A method inherited from parent class can be
replaced in the derived class by marking its
definition with new
– The parent class version can still be called
explicitly with the prefix base
• Dynamic binding
– To allow dynamic binding of method calls to
methods:
• The base class method is marked virtual
• The corresponding methods in derived classes are
marked override
– Abstract methods are marked abstract and
must be implemented in all subclasses
– All C# classes are ultimately derived from a single
root class, Object
• Evaluation
– C# is the most recently designed C-based OO
language
– The differences between C#’s and Java’s support
for OOP are relatively minor
• General Characteristics
– OOP was one of the most important extensions to
Ada 83
– Encapsulation container is a package that defines a
tagged type
– A tagged type is one in which every object
includes a tag to indicate during execution its type
(the tags are internal)
– Tagged types can be either private types or records
– No constructors or destructors are implicitly called
• Inheritance
– Subclasses can be derived from tagged types
– New entities in a subclass are added in a record
• Inheritance (continued)
– All subclasses are subtypes
– Single inheritance only, except through generics
• Dynamic Binding
– Dynamic binding is done using polymorphic
variables called classwide types
– e.g., for the tagged type PERSON, the classwide
type is PERSON’class
– Other bindings are static
– Any method may be dynamically bound
• JavaScript Objects
– An object has a collection of properties, which are
either data properties or method properties
– Appear as hashes, both internally and externally
– A list of property/value pairs
– Properties can be added or deleted dynamically
– A bare object can be created with new and a call to
the constructor for Object
var my_object = new Object();
– References to properties are with dot notation