• No results found

Object Oriented Languages Multiple Inheritance

N/A
N/A
Protected

Academic year: 2021

Share "Object Oriented Languages Multiple Inheritance"

Copied!
40
0
0

Loading.... (view fulltext now)

Full text

(1)

Object Oriented Languages

Multiple Inheritance

Now there is another question to tackle: method (and attribute) selection when we have multiple inheritance in the class hierarchy

This applies to both single and multiple dispatch method calls In single inheritance the job is easy: if the class of the current object has a method defined, use it; else recurse to the parent class

But with MI you can inherit behaviour or structure from more than one parent

And when you have more than one parent, how do you order the superclasses when determining what to inherit from?

(2)

Object Oriented Languages

Multiple Inheritance

Now there is another question to tackle: method (and attribute) selection when we have multiple inheritance in the class hierarchy

This applies to both single and multiple dispatch method calls

In single inheritance the job is easy: if the class of the current object has a method defined, use it; else recurse to the parent class

But with MI you can inherit behaviour or structure from more than one parent

And when you have more than one parent, how do you order the superclasses when determining what to inherit from?

(3)

Object Oriented Languages

Multiple Inheritance

Now there is another question to tackle: method (and attribute) selection when we have multiple inheritance in the class hierarchy

This applies to both single and multiple dispatch method calls In single inheritance the job is easy: if the class of the current object has a method defined, use it; else recurse to the parent class

But with MI you can inherit behaviour or structure from more than one parent

And when you have more than one parent, how do you order the superclasses when determining what to inherit from?

(4)

Object Oriented Languages

Multiple Inheritance

Now there is another question to tackle: method (and attribute) selection when we have multiple inheritance in the class hierarchy

This applies to both single and multiple dispatch method calls In single inheritance the job is easy: if the class of the current object has a method defined, use it; else recurse to the parent class

But with MI you can inherit behaviour or structure from more than one parent

And when you have more than one parent, how do you order the superclasses when determining what to inherit from?

(5)

Object Oriented Languages

Multiple Inheritance

Now there is another question to tackle: method (and attribute) selection when we have multiple inheritance in the class hierarchy

This applies to both single and multiple dispatch method calls In single inheritance the job is easy: if the class of the current object has a method defined, use it; else recurse to the parent class

But with MI you can inherit behaviour or structure from more than one parent

And when you have more than one parent, how do you order the superclasses when determining what to inherit from?

(6)

Object Oriented Languages

Multiple Inheritance

B C

A D

Suppose a methodsleepis defined in bothBandC, but notA

Ifsleepis called with argument in classAshould it use the method fromBorC?

B, perhaps, as that is on the left, and we read left-to-right? But other people read right-to-left, and what if we had happened to draw the same hierarchy in a different way?

(7)

Object Oriented Languages

Multiple Inheritance

B C

A D

Suppose a methodsleepis defined in bothBandC, but notA

Ifsleepis called with argument in classAshould it use the method fromBorC?

B, perhaps, as that is on the left, and we read left-to-right? But other people read right-to-left, and what if we had happened to draw the same hierarchy in a different way?

(8)

Object Oriented Languages

Multiple Inheritance

B C

A D

Suppose a methodsleepis defined in bothBandC, but notA

Ifsleepis called with argument in classAshould it use the method fromBorC?

B, perhaps, as that is on the left, and we read left-to-right? But other people read right-to-left, and what if we had happened to draw the same hierarchy in a different way?

(9)

Object Oriented Languages

Multiple Inheritance

B C

A D

Suppose a methodsleepis defined in bothBandC, but notA

Ifsleepis called with argument in classAshould it use the method fromBorC?

B, perhaps, as that is on the left, and we read left-to-right?

But other people read right-to-left, and what if we had happened to draw the same hierarchy in a different way?

(10)

Object Oriented Languages

Multiple Inheritance

A D

C B

Suppose a methodsleepis defined in bothBandC, but notA

Ifsleepis called with argument in classAshould it use the method fromBorC?

B, perhaps, as that is on the left, and we read left-to-right? But other people read right-to-left, and what if we had happened to draw the same hierarchy in a different way?

(11)

Object Oriented Languages

Multiple Inheritance

B C

A D

Or supposesleepis (only) defined inDandC

Going up the hierarchy on the left we get toDfirst; going up on the right we get toCfirst

Doing a breadth-first search, we findCfirst What shouldAdo?

(12)

Object Oriented Languages

Multiple Inheritance

B C

A D

Or supposesleepis (only) defined inDandC

Going up the hierarchy on the left we get toDfirst; going up on the right we get toCfirst

Doing a breadth-first search, we findCfirst What shouldAdo?

(13)

Object Oriented Languages

Multiple Inheritance

B C

A D

Or supposesleepis (only) defined inDandC

Going up the hierarchy on the left we get toDfirst; going up on the right we get toCfirst

Doing a breadth-first search, we findCfirst

(14)

Object Oriented Languages

Multiple Inheritance

B C

A D

Or supposesleepis (only) defined inDandC

Going up the hierarchy on the left we get toDfirst; going up on the right we get toCfirst

Doing a breadth-first search, we findCfirst What shouldAdo?

(15)

Object Oriented Languages

Multiple Inheritance

B C

A D

This is called the diamond problem

When there is more than one candidate method to call, how do we (actually the compiler or interpreter) choose which one?

(16)

Object Oriented Languages

Multiple Inheritance

B C

A D

This is called the diamond problem

When there is more than one candidate method to call, how do we (actually the compiler or interpreter) choose which one?

(17)

Object Oriented Languages

Multiple Inheritance

There have been many attempts to address this question

Every MI language needs a way of choosing, or forcing the programmer to choose

Some languages force you to disambiguate yourself, e.g.,

D::sleep()

Many languages have a built-in algorithm to choose for you (see linearisation, below)

(18)

Object Oriented Languages

Multiple Inheritance

There have been many attempts to address this question Every MI language needs a way of choosing, or forcing the programmer to choose

Some languages force you to disambiguate yourself, e.g.,

D::sleep()

Many languages have a built-in algorithm to choose for you (see linearisation, below)

(19)

Object Oriented Languages

Multiple Inheritance

There have been many attempts to address this question Every MI language needs a way of choosing, or forcing the programmer to choose

Some languages force you to disambiguate yourself, e.g.,

D::sleep()

Many languages have a built-in algorithm to choose for you (see linearisation, below)

(20)

Object Oriented Languages

Multiple Inheritance

There have been many attempts to address this question Every MI language needs a way of choosing, or forcing the programmer to choose

Some languages force you to disambiguate yourself, e.g.,

D::sleep()

Many languages have a built-in algorithm to choose for you (see linearisation, below)

(21)

Object Oriented Languages

Multiple Inheritance

But does this algorithmic choice reflect the expectations of the programmer?

Usually yes in simple cases, but what about more complex hierarchies?

The fact there are many linearisation algorithms tells us something!

(22)

Object Oriented Languages

Multiple Inheritance

But does this algorithmic choice reflect the expectations of the programmer?

Usually yes in simple cases, but what about more complex hierarchies?

The fact there are many linearisation algorithms tells us something!

(23)

Object Oriented Languages

Multiple Inheritance

But does this algorithmic choice reflect the expectations of the programmer?

Usually yes in simple cases, but what about more complex hierarchies?

The fact there are many linearisation algorithms tells us something!

(24)

Object Oriented Languages

Multiple Inheritance

For example, in simple cases, Common Lisp makes a choice by looking at how the classes were defined

(25)

Object Oriented Languages

Multiple Inheritance

If the definition was (defclass D () ...) (defclass B (D) ...) (defclass C (D) ...) (defclass A (B C) ...)

it might order the superclasses ofAas(A B C D). This is called a linearisation of the superclasses

And the resulting order(A C B D)is called the class precedence list (CPL) forA

Thus a method defined inBis preferred over one defined inC

(26)

Object Oriented Languages

Multiple Inheritance

If the definition was (defclass D () ...) (defclass B (D) ...) (defclass C (D) ...) (defclass A (B C) ...)

it might order the superclasses ofAas(A B C D). This is called a linearisation of the superclasses

And the resulting order(A C B D)is called the class precedence list (CPL) forA

Thus a method defined inBis preferred over one defined inC

(27)

Object Oriented Languages

Multiple Inheritance

If the definition was (defclass D () ...) (defclass B (D) ...) (defclass C (D) ...) (defclass A (B C) ...)

it might order the superclasses ofAas(A B C D). This is called a linearisation of the superclasses

And the resulting order(A C B D)is called the class precedence list (CPL) forA

Thus a method defined inBis preferred over one defined inC

(28)

Object Oriented Languages

Multiple Inheritance

If the definition was (defclass D () ...) (defclass B (D) ...) (defclass C (D) ...) (defclass A (B C) ...)

it might order the superclasses ofAas(A B C D). This is called a linearisation of the superclasses

And the resulting order(A C B D)is called the class precedence list (CPL) forA

Thus a method defined inBis preferred over one defined inC

(29)

Object Oriented Languages

Multiple Inheritance

On the other hand, if we happened to define (defclass D () ...)

(defclass B (D) ...) (defclass C (D) ...) (defclass A (C B) ...) the CPL would be(A C B D)

This makes the resolution ofBversusCconsistent with the (perhaps unconscious) choice of the programmer

(30)

Object Oriented Languages

Multiple Inheritance

On the other hand, if we happened to define (defclass D () ...)

(defclass B (D) ...) (defclass C (D) ...) (defclass A (C B) ...) the CPL would be(A C B D)

This makes the resolution ofBversusCconsistent with the (perhaps unconscious) choice of the programmer

(31)

Object Oriented Languages

Multiple Inheritance

A class precedence list helps the language decide which method to use

It gives us the method resolution order (MRO)

Namely the ordering of the applicable methods so we (a) can pick the right method and (b) have an ordered list of methods for method composition

(32)

Object Oriented Languages

Multiple Inheritance

A class precedence list helps the language decide which method to use

It gives us the method resolution order (MRO)

Namely the ordering of the applicable methods so we (a) can pick the right method and (b) have an ordered list of methods for method composition

(33)

Object Oriented Languages

Multiple Inheritance

A class precedence list helps the language decide which method to use

It gives us the method resolution order (MRO)

Namely the ordering of the applicable methods so we (a) can pick the right method and (b) have an ordered list of methods for method composition

(34)

Object Oriented Languages

Multiple Inheritance

In object-receiver languages, usually the method chosen is the earliest found following the CPL

Thus if the CPL is(A B C D)and bothBandCdefinesleep, then pick the method fromB

(35)

Object Oriented Languages

Multiple Inheritance

In object-receiver languages, usually the method chosen is the earliest found following the CPL

Thus if the CPL is(A B C D)and bothBandCdefinesleep, then pick the method fromB

(36)

Object Oriented Languages

Multiple Inheritance

In object-receiver languages, usually the method chosen is the earliest found following the CPL

Thus if the CPL is(A B C D)and bothBandCdefinesleep, then pick the method fromB

(37)

Object Oriented Languages

Multiple Inheritance

Or methods if we have method composition

This is why we need the whole CPL, not just a single class It’s not the whole story if we have multiple method dispatch as we have extra complication over multiple argument classes With object receiver, the MRO is just the CPL; with

(38)

Object Oriented Languages

Multiple Inheritance

Or methods if we have method composition

This is why we need the whole CPL, not just a single class

It’s not the whole story if we have multiple method dispatch as we have extra complication over multiple argument classes With object receiver, the MRO is just the CPL; with

(39)

Object Oriented Languages

Multiple Inheritance

Or methods if we have method composition

This is why we need the whole CPL, not just a single class It’s not the whole story if we have multiple method dispatch as we have extra complication over multiple argument classes

With object receiver, the MRO is just the CPL; with multimethods calculating the MRO is harder

(40)

Object Oriented Languages

Multiple Inheritance

Or methods if we have method composition

This is why we need the whole CPL, not just a single class It’s not the whole story if we have multiple method dispatch as we have extra complication over multiple argument classes With object receiver, the MRO is just the CPL; with

References

Related documents

data only to some extent (median absolute difference in the strongest gradient: 6–7 µmol kg −1 ; Fig. 3h) and is much more important for the unpumped 4330 optode with larger re-..

We offer a wide range of undergraduate, graduate, and professional degree programs in a variety of disciplines – from Jewish studies to education to law and to the health sciences -

Nortel VPN Gateways provide dynamic access policy management to ensure simplified yet highly secure provisioning of users and groups with the enterprise.. The gateways also

Sama seperti lapisan masyarakat di Desa Beringin Agung, dapat disimpulkan bahwa masing-masing rumahtangga petani di Desa Pendahara cenderung memilih mencari ikan di sungai sebagai

Based on the conditional Gaussian approximation technique, we derive the BER formulas for BPSK and 16-QAM modulated OFDM signals impaired by phase noise in frequency-selective

 If there is an inheritance relationship, the child class object is also. identified as an object of the

Answer: Various elements of OOP are: Object Class Method Encapsulation Information Hiding Inheritance Polymorphism Question: What are the characteristics of Object-..

Create a control for the stop and your block diagram should look like the one pictured in Figure 15.. We are almost ready to gather some data, but we need to close