Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

LECTURE 8

Support for OOP

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Categories of languages that support OOP:

1. OOP support is added to an existing language


• C++ (also supports procedural and data oriented programming)
• Ada 95 (also supports procedural and data oriented programming)
• CLOS (also supports functional programming)
• Scheme (also supports functional programming)
2. Support OOP, but have the same appearance and use the basic
structure of earlier imperative languages
• Eiffel (not based directly on any previous language)
• Java (based on C++)
3. Pure OOP languages
• Smalltalk
Concepts of Programming Languages - NUN 2024 Austin Olom Ogar
Paradigm Evolution

1. Procedural - 1950s-1970s (procedural abstraction)


2. Data-Oriented - early 1980s (data-oriented)
3. OOP - late 1980s (Inheritance and dynamic binding)

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Object-Oriented Programming

• Object-oriented programming is, in essence, an application of the


principle of abstraction to abstract data types.
• Specifically, in object-oriented programming, the commonality of a
collection of similar abstract data types is factored out and put in a
new type.
• The members of the collection inherit these common parts from that
new type. This feature is inheritance, which is at the center of object-
oriented programming and the languages that support it.

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Object-Oriented Programming

• The concept of object-oriented programming had its roots in SIMULA


67 but was not fully developed until the evolution of Smalltalk
resulted in Smalltalk 80.
• Some consider Smalltalk to be the base model for a purely object-
oriented programming language.
• A language that is object oriented must provide support for three key
language features: abstract data types, inheritance, and dynamic
binding of method calls to methods.

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Origins of Inheritance

• Observations of the mid-late 1980s :


• Productivity increases can come from reuse
• 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

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


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
• The calls to methods are sometimes called messages.
• The entire collection of methods of a class is called the message protocol, or
message interface, of the class
• Messages have two parts--a method name and the destination object
• In the simplest case, a class inherits all of the entities of its parent
Concepts of Programming Languages - NUN 2024 Austin Olom Ogar
OOP Definitions (cont.)
• Inheritance can be complicated by access controls to encapsulated entities
• A class can hide entities from its subclasses
• A class can hide entities from its clients
• Besides inheriting methods as it is, a class can modify an inherited method
• The new one overrides the inherited one
• The method in the parent is overriden
• There are two kinds of variables in a class:
1. Class variables - one/class
2. Instance variables - one/object
• There are two kinds of methods in a class:
1. Class methods - messages to the class
2. Instance methods - messages to objects
• Single vs. Multiple Inheritance

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Single and Multiple Inheritances
• If a new class is a subclass of a single parent class, then the derivation
process is called single inheritance.
• If a class has more than one parent class, the process is called
multiple inheritance.

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Disadvantage of inheritance for reuse

• Creates interdependencies among classes that complicate maintenance


• This result works against one of the advantages of abstract data types, which is that they are
independent of each other. But in general, the independence of abstract data types is one of
their strongest positive characteristics. However, it may be difficult, if not impossible, to
increase the reusability of abstract data types without creating dependencies among some of
them.

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


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
• A virtual method is one that does not include a definition (it only defines
a protocol)
• A virtual class is one that includes at least one virtual method
• A virtual class cannot be instantiated

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Dynamic Binding

• The third essential characteristic (after abstract data types and


inheritance) of object-oriented programming languages is a kind of
polymorphism provided by the dynamic binding of messages to
method definitions.
• This is sometimes called dynamic dispatch. Consider the following
situation: There is a base class, A, that defines a method draw that
draws some figure associated with the base class. A second class, B, is
defined as a subclass of A.

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Dynamic Binding (cont.)

• Objects of this new class also need a draw method that is like that
provided by A but a bit different because the subclass objects are
slightly different.
• So, the subclass overrides the inherited draw method. If a client of A
and B has a variable that is a reference to class A’s objects, that
reference also could point at class B’s objects, making it a
polymorphic reference. If the method draw, which is defined in both
classes, is called through the polymorphic reference, the run-time
system must determine, during execution, which method should be
called, A’s or B’s

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Design Issues for OOPLs

1. The Exclusivity of Objects


a. Everything is an object
advantage - elegance and purity
disadvantage - slow operations on simple objects (e.g., float)
b. Add objects to a complete typing system
Advantage - fast operations on simple objects
Disadvantage - results in a confusing type system
c. 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

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Are Subclasses Subtypes?

• Is there a relationship between a parent class object and an object of


the subclass?

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Implementation and Interface Inheritance

• If only the interface of the parent class is visible to the subclass, it is


interface inheritance
• Disadvantage - can result in inefficiencies
• If both the interface and the implementation of the parent class is
visible to the subclass, it is implementation inheritance
• Disadvantage - changes to the parent class require recompilation of
subclasses, and sometimes even modification of subclasses

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Type Checking and Polymorphism

• Polymorphism may require dynamic type checking of parameters and


the return value
• Dynamic type checking is costly and delays error detection
• If overriding methods are restricted to having the same parameter
types and return type, the checking can be static

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Single and Multiple Inheritance

• Disadvantage of multiple inheritance:


• Language and implementation complexity
• Potential inefficiency - dynamic binding costs more with multiple inheritance

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Allocation and Deallocation of Objects

• From where are objects allocated?


• If they all live in the heap, references to them are uniform
• Is deallocation explicit or implicit?

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Dynamic and Static Binding

• Should ALL binding of messages to methods be dynamic?

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Support for Object-Oriented
Programming in Specific Languages

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Overview of Smalltalk

• Smalltalk is a pure OOP language


• Everything is an object
• All computation is through objects sending messages to objects
• It adopts none of the appearance of imperative languages
• The Smalltalk Environment
• The first complete GUI system
• A complete system for software development
• All of the system source code is available to the user, who can modify it if
he/she wants

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


General Characteristics

• In Smalltalk, the concept of an object is truly universal. Virtually


everything, from things as simple as the integer constant to a
complex file-handling system, is an object.
• As objects, they 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.
• Classes cannot be nested in Smalltalk.

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


General Characteristics

• All computation is through messages, even a simple arithmetic


operation.
• For example, the expression x + 7 is implemented as sending the + message to
x (to enact the + method), sending 7 as the parameter. This operation returns
a new numeric object with the result of the addition.
• All Smalltalk objects are allocated from the heap and are referenced
through reference variables, which are implicitly dereferenced.

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


General Characteristics

• There is no explicit deallocation statement or operation. So, all deallocation


is implicit, using a garbage collection process for storage reclamation.
• In Smalltalk, constructors must be explicitly called when an object is
created. A class can have multiple constructors, but each must have a
unique name.
• Smalltalk classes cannot be nested in other classes. Unlike hybrid languages
such as C++ and Objective-C.
• Smalltalk was designed for just one software development paradigm—
object oriented. Furthermore, it adopts none of the appearance of the
imperative languages. Its purity of purpose is reflected in its simple
elegance and uniformity of design.

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Inheritance

• A Smalltalk subclass inherits all of the members of its superclass.


• The subclass can also have its own instance variables, which must have names that are
distinct from the variable names in its ancestor classes.
• The subclass can define new methods and redefine methods that already exist in an
ancestor class.
• When a subclass has a method whose name and protocol are the same as an ancestor
class, the subclass method hides that of the ancestor class.
• Access to such a hidden method is provided by prefixing the message with the
pseudovariable super. The prefix causes the method search to begin in the superclass
rather than locally.
• Because members in a parent class cannot be hidden from subclasses, subclasses can be
and usually are subtypes.
• Smalltalk does not support multiple inheritance.
Concepts of Programming Languages - NUN 2024 Austin Olom Ogar
Dynamic Binding

• The dynamic binding of messages to methods in Smalltalk operates as


follows:
• A message to an object causes a search of the class to which the object
belongs for a corresponding method. If the search fails, it is continued in the
superclass of that class, and so forth, up to the system class, Object, which
has no superclass.
• Object is the root of the class derivation tree on which every class is a node. If
no method is found anywhere in that chain, an error occurs. It is important to
remember that this method search is dynamic— It takes place when the
message is sent.
• Smalltalk does not, under any circumstances, bind messages to methods
statically.

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Dynamic Binding

• The only type checking in Smalltalk is dynamic, and the only type error occurs
when a message is sent to an object that has no matching method, either locally
or through inheritance.
• This is a different concept of type checking than that of most other languages.
Smalltalk type checking has the simple goal of ensuring that a message matches
some method.
• Smalltalk variables are not typed; any name can be bound to any object.
• As a direct result, Smalltalk supports dynamic polymorphism. All Smalltalk code is
generic in the sense that the types of the variables are irrelevant, as long as they
are consistent.
• The meaning of an operation (method or operator) on a variable is determined by
the class of the object to which the variable is currently bound.
Concepts of Programming Languages - NUN 2024 Austin Olom Ogar
Evaluation of Smalltalk

• Smalltalk is a small language, although the Smalltalk system is large. The syntax of
the language is simple and highly regular.
• It is a good example of the power that can be provided by a small language if that
language is built around a simple but powerful concept. In the case of Smalltalk,
that concept is that all programming can be done employing only a class
hierarchy built using inheritance, objects, and message passing
• In comparison with conventional compiled imperative- language programs,
equivalent Smalltalk programs are significantly slower.
• Although it is theoretically interesting that array indexing and loops can be
provided within the message- passing model, efficiency is an important factor in
the evaluation of programming languages. Therefore, efficiency will clearly be an
issue in most discussions of the practical applicability of Smalltalk.

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


C++

• General Characteristics:
• Mixed typing system
• Constructors and destructors
• Elaborate access controls to class entities
• Inheritance
• A class need not be subclasses of any class
• Access controls for members are
1. Private (visible only in the class and friends)
2. Public (visible in subclasses and clients)
3. Protected (visible in the class and in subclasses)
• In addition, the subclassing process can be declared with access controls, which define
potential changes in access by subclasses
• Multiple inheritance is supported

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar

You might also like