Support For Object-Oriented Programming
Support For Object-Oriented Programming
Support For Object-Oriented Programming
Support for
Object-Oriented
Programming
Introduction
• Many object-oriented programming (OOP)
languages
– Some support procedural and data-oriented
programming (e.g., Ada 95+ and C++)
– Some support functional program (e.g., CLOS)
– Newer languages do not support other
paradigms but use their imperative structures
(e.g., Java and C#)
– Some are pure OOP language (e.g., Smalltalk &
Ruby)
– Some functional languages support OOP, but
they are not discussed in this chapter
Copyright © 2012 Addison-Wesley. All rights reserved. 1-2
Object-Oriented Programming
• Everything is an object
– Advantage - elegance and purity
– Disadvantage - slow operations on simple objects
• Add objects to a complete typing system
– Advantage - fast operations on simple objects
– Disadvantage - results in a confusing type system (two
kinds of entities)
• Include an imperative-style typing system for
primitives but make everything else objects
– Advantage - fast operations on simple objects and a
relatively small typing system
– Disadvantage - still some confusion because of the two
type systems
• Inheritance
– A Smalltalk subclass inherits all of the instance
variables, instance methods, and class methods
of its superclass
– All subclasses are subtypes (nothing can be
hidden)
– All inheritance is implementation inheritance
– No multiple inheritance
• 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
– Introduced the graphical user interface
– Greatest impact: advancement of OOP
• Inheritance
– A class need not be the subclass of any class
– Access controls for members are
– Private (visible only in the class and friends)
(disallows subclasses from being subtypes)
– Public (visible in subclasses and clients)
– Protected (visible in the class and in subclasses,
but not clients)
• 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 controls (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++
• Dynamic Binding
– In Java, all messages are dynamically bound to
methods, unless the method is final (i.e., it
cannot be overriden, therefore dynamic binding
serves no purpose)
– Static binding is also used if the methods is
static or private both of which disallow
overriding
• Evaluation
– Design decisions to support OOP are similar to
C++
– No support for procedural programming
– No parentless classes
– Dynamic binding is used as “normal” way to
bind method calls to method definitions
– Uses interfaces to provide a simple form of
support for multiple inheritance
• 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 (e.g., no inheritance)
• 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:
base.Draw()
• 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 a relatively 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
Copyright © 2012 Addison-Wesley. All rights reserved. 1-42
Support for OOP in Ada 95 (continued)
• Inheritance
– Subclasses can be derived from tagged types
– New entities are added to the inherited entities
by placing them in a record definition
– All subclasses are subtypes
– No support for multiple inheritance
• A comparable effect can be achieved using generic
classes
• Dynamic Binding
– Dynamic binding is done using polymorphic
variables called classwide types
• For the tagged type Person, the classwide type is
Person‘ class
– Other bindings are static
– Any method may be dynamically bound
– Purely abstract base types can be defined in Ada
95 by including the reserved word abstract
• Child Packages
– A child package is logically (possibly physically)
nested inside another package; if separate, they
are called child library packages
– Solves the problem of packages becoming
physically too large
– Even the private parts of the parent package are
visible to the child package
– A child package is an alternative to class
derivation
– A child library package can be added any time
to a program
Copyright © 2012 Addison-Wesley. All rights reserved. 1-47
Support for OOP in Ada 95 (continued)
• Evaluation
– Ada offers complete support for OOP
– C++ offers better form of inheritance than Ada
– Ada includes no initialization of objects (e.g.,
constructors)
– Dynamic binding in C-based OOP languages is
restricted to pointers and/or references to
objects; Ada has no such restriction and is thus
more orthogonal