Docsity

Object-Oriented Languages: Encapsulation, Inheritance, and Polymorphism, Study notes of Computer Science

Partial preview of the text

Download Object-Oriented Languages: Encapsulation, Inheritance, and Polymorphism and more Study notes 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

[email protected]

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

Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages

Encapsulation

Inheritance

Polymorphism

Typing and Object-Orientation

Object-Based Languages

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.

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.

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

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?

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.

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

Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages

Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation

Problems with Multiple Inheritance

Multiple inheritance is not without its problems. The biggest have

to do with duplication of functionality and data (so-called repeated

inheritance):

◮ Functionality duplication: how do you figure out which

method to call when two base classes have methods with the

same name? or which field to access when two fields of the

same name exist?

◮ Data duplication: what happens when we inherit the same

data member from two different base classes?

Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages

Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation

Problem: Functionality Duplication

1 class CardGame {

3 draw() { ... } // draw a card

7 class GraphicsObject {

9 draw() { ... } // draw the object

13 class GraphicalCardGame

14 inherits CardGame, GraphicsObject {

Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages

Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation

Problem: Data Duplication

◮ If we have a field defined in

C1, how many copies are in

C4?

◮ May want one copy, may

want more than one

◮ Also know as the “diamond

problem” (or the more

dramatic “diamond of

death”!)

C

C2 C

C

Encapsulation Inheritance Polymorphism Typing and Object-Orientation Object-Based Languages

Inheritance Overview Single Inheritance Multiple Inheritance Other Inheritance Models Inheritance, Overrides, and Encapsulation

Possible Solutions

◮ Always just include one copy

◮ Always include all the copies

◮ Allow programmer to choose (C++)

Note the first two solutions will exclude some valid programs, since

we may want just one, or more than one, copy in various situations.

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

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