Download Understanding Object-Oriented Languages: Encapsulation, Inheritance, Polymorphism, Typing and more Papers Computer Science in PDF only on Docsity!
Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
CS421 Lecture 19: Object-Oriented Languages
Mark Hills
University of Illinois at Urbana-Champaign
July 24, 2006
Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul
Agha, and Elsa Gunter
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Encapsulation
Inheritance
Polymorphism
Typing and Object-Orientation
Object-Based Languages
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Objectives
By the end of this lecture, you should
ā® be familiar with the core concepts of object- oriented
languages
ā® understand different design decisions made in designing OO
languages
ā® understand different methods of typing object- oriented
languages, including some of the theory behind why this is
challenging
ā® know the difference between object (or prototype) languages
and class-based, object-oriented languages
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Object-Oriented Languages
Object-oriented (OO) languages are those based around the
concepts of:
ā® The class, an abstract entity which specifies data and
functionality related to that data; and
ā® the object, an instance of a particular class which actually
contains the specified data and provides something for the
specified functionality to work on.
OO languages can be seen as an extension of ADT mechanisms,
but with some additional features.
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
OO Languages ā Core Concepts
For a language to be an OO language it generally must support the
following three concepts:
ā® Encapsulation: The language should have some method for
containing and potentially hiding parts of definitions, similar
to those present in module systems and used with ADTs;
ā® Inheritance: It should be possible for one class to inherit
functionality from one or more other classes, allowing for
reuse of existing functionality;
ā® Polymorphism: Different objects should be able to respond
to the same request in different ways; essentially, we should be
able to ask objects to ādo somethingā, allowing them to figure
out how to do it.
Different languages approach these three points in different ways.
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Encapsulation
Encapsulation is part of what makes objects objects, instead of
just ADTs (caveat: some ADTs, such as in Euclid, have similar
functionality, with ADTs as types):
ā® Each object contains data associated to it;
ā® each object contains functionality that knows how to operate
over that data;
ā® objects ask other objects to do things for them by calling
methods or sending messages.
Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Basic Encapsulation
At its most basic, encapsulation just allows data members (also
properties or fields) and function members (methods) to be
declared as part of a class.
ā® Note that this does not give us any data hiding;
ā® this also doesnāt give us any āinternalā functionality ā any
object could call a method on any other object
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Encapsulation Options
A range of choices about how to handle encapsulation is available
in current OO languages:
ā® are fields visible outside a class?
ā® can objects of a class see inside other objects of the same
class?
ā® is it possible to set different levels of visibility on fields and
methods?
ā® do the visibility mechanisms interact with the inheritance
mechanism?
ā® do the visibility mechanisms interact with other
naming/grouping mechanisms?
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation
Inheritance
In OO languages, inheritance allows a class to reuse functionality
from (usually) another class, without having to rewrite or
copy/paste the code.
ā® The parent, base, or super class is the class being inherited
from.
ā® The child, derived, or sub class is the class doing the
inheriting.
ā® The child class can reuse the functionality from the parent
class, including functionality the parent got from its parent,
etc.
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation
Types of Inheritance
ā® Some languages only allow a class to inherit from one other
class ā this is single inheritance.
ā® Some languages allow a class to inherit from multiple classes,
which is multiple inheritance.
ā® An alternate model of inheritance allows functionality to be
pulled out into distinct units which can then be used to form
new classes. These units of reuse are called mixins.
ā® Mixins arguably introduce some complications, which traits
are intended to solve.
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation
Single Inheritance
Single-inheritance languages only allow a class to inherit from one
other class.
ā® Examples include Java, C#, and Smalltalk
ā® Generally includes a designated base class from which all
other classes inherit (Object in Java)
ā® Semantically cleaner
ā® Easier to predict behavior
ā® Easier to translate/compile
ā® Cannot model some situations
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation
Multiple Inheritance
Multiple inheritance allows a single class to have multiple base
classes.
ā® Examples include C++ and Eiffel
ā® Classes do not necessarily inherit from another class (in C++,
classes need not have a parent class)
ā® Can model more situations
ā® Semantically more confusing
ā® Harder to correctly compile
Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation
Mixins
An alternate model of reuse is provided by mixins.
ā® mixins provide specific pieces of state and functionality
ā® new classes are created by adding mixins to existing classes
ā® order is important ā if two mixins provide the same method
name, the last one wins
ā® can lead to brittle schemes where no proper ordering of the
mixins is possible
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation
Traits
Traits were introduces in Smalltalk to improve on the mixin model
and allow some benefits of multiple inheritance without the cost.
ā® Traits only provide functionality ā no state
ā® Traits access needed state through methods
ā® Each trait lists which methods it needs and which it provides
ā® Trait composition then needs to resolve all the required
methods
ā® Traits allow hiding of methods and renaming to resolve
conflicts
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation
Overloading and Overriding
Inheritance allows methods in derived classes to override methods
in base classes:
ā® New method should have same signature as original method;
ā® new method hides original method in some cases, based on
language rules
A method with the same name but a different signature is said to
overload that name, giving it multiple meanings:
ā® Overload rules differ between languages, including what can
be a valid overload
ā® Overload interaction with inheritance also differs between
languages ā what happens if you overload an inherited name?
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation
Using Inherited Functionality
ā® Non-overridden methods and fields can be directly referenced
(based on visibility rules)
ā® Base-class methods can usually be called using keywords
(super.method in Java) or scope resolution
(MyBase::method in C++)
ā® These are based on the visibility rules from encapsulation ā in
Java, private methods from a parent arenāt suddenly public
in a child, for instance, so the child cannot see them.
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation
Alternate Model ā Beta
The Beta language uses an alternate model. Instead of calling the
method and using super to call the parent definition, the
definition at the root of the inheritance hierarchy is called first and
inner is used to call back towards the child.
ā® Note that this makes calling the child optional!
ā® Helps to support behavioral subtyping ā the child cannot
ignore the parent, unlike in languages with super but not
inner, so the parent can enforce behavior
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Polymorphism
Polymorphism is the third key feature of OO languages. At its
core, polymorphism provides the mechanism for letting objects of
different classes figure out how to do things themselves ā a Circle
object would draw itself differently than a Triangle object, for
instance, but I should be able use use them both as Graphics
objects.
Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Methods of Enabling Polymorphism
Polymorphism can be enabled in two major ways:
ā® Structurally: especially in dynamically typed or untyped
languages, if I have a reference to an object I can send it a
message, and if it understands it then it can take action
ā® Subtype Polymorphism ā especially in statically typed
languages, if class A inherits from class B then an object of
class A should be usable wherever an object of class B can be
used, but I should be able to ācustomizeā class A to respond
differently to the same requests
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Virtual Methods
The key to getting subtype polymorphism to work is that the
method to invoke should be choosable at runtime. This way the
behavior can vary, based on the type of the object. Often methods
that allow this are called virtual methods.
ā® Virtual comes from virtual function table, the structure often
used to resolve the correct function to invoke;
ā® in some languages (like Java) all methods are virtual;
ā® in other languages (like C++) methods can be static or
virtual; the former has better performance;
ā® this way of selecting methods is also called dynamic
dispatch.
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Method Selection
In the presence of dynamic dispatch, the way to choose the proper
method to invoke varies from language to language:
ā® The proper virtual method is chosen at runtime based on the
dynamic type of the object;
ā® the proper static method is chosen at compile time based on
the static type of the variable holding the object
(pointer/reference);
ā® some languages have more involved models ā multimethods in
CLOS or predicate dispatch (a current research topic).
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
OO Type Systems
Type systems for object-oriented languages are generally more
complex than for imperative languages because of two factors:
ā® Recursive Types: All types are recursive, with methods
requiring implicit or explicit self or this pointers.
ā® Subtyping: Most languages with subclassing use this
mechanism to establish subtypes as well; the subtypes are
constrained in certain ways to ensure that the language
remains type safe.
Recursive types have been more of a problem in the theory behind
typing OO languages; subtyping has led to more problems in actual
implementations of type checkers.
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Subtyping
There is a fair amount of theory behind subtyping. For our
purposes, we need to be concerned with two main features of the
subtyping relation, as it applies here:
ā® For class A to be a valid subtype of class B, the fields of class
A must have the same types as those of class B
ā® For class A to be a valid subtype of class B, each method of
class A which overrides a method of class B must be a
subtype of the overridden method
ā® For method F to be a subtype of method G each parameter of
F must be the same or a supertype of the same parameter in
G and the return type of F must be the same or a subtype of
the return type of G
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Terminology
This rule for method subtyping is often stated as
ā® the parameter types are contravariant,
ā® while the return type is covariant
Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Why do I need classes?
Some languages forego using classes altogether. These languages
are often called just object languages, or prototype languages.
ā® New objects are created by cloning existing objects;
ā® objects contain slots that hold either data members or
methods;
ā® operations can be performed on an object to change the
values in slots, add or remove slots, etc;
ā® various languages have different ways to access to parent to
permit delegation, a way to request functionality from
another object
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Method Calls in Prototype Languages
One major different with languages such as SELF and standard OO
languages is that methods are executed in the context of the caller
ā i.e. not in the context of the actual owner of the method. The
method is found and returned, and then has access to the state of
the object that called it.
Mark Hills CS421 Lecture 19: Object-Oriented Languages
Outline Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages
Further Reading
This is an area of current research, and these languages are not
widely used. A good reference is:
ā® The Interpretation of Object-Oriented Programming
Languages, by Iain Craig.
Mark Hills CS421 Lecture 19: Object-Oriented Languages