Polymorphism in languages and its implications (Smalltalk, Modula-3, Java, and C++)

Cours polymorphism in languages and its implications, tutoriel & guide de travaux pratiques en pdf.

Modula-3
• Polymorphism is a direct consequence of subtyping
• Example: TYPE printableBag = OBJECT values: ARRAY OF printableThing; METHODS add(v: printableThing) … VAR myThings: printableBag; myThings.Add(new point); myThings.Add(new Rect)
Example (Cont.)
• FOR i = 0 TO LAST(myThings.values) DO myThing.values[i].print()
• How is this different from Smalltalk?
• What kind of polymorphism is this in Cardelli and Wegner’s terminology?

How to implement method dispatch?

• Can do it like Smalltalk but can we exploit – Static typing?
• Possible types of object is constrained statically – Single inheritance?
• Exactly one immediate supertype

Tables

• Idea: – Prepend the methods of a supertype to a subtype – A method T::m appears in the same position in all T’s subclasses
Java
• Similar to Modula-3 except for invokeinterface
• Example: interface hasAPrint { void print(); } interface hasASize { void size(); } class text implements hasAPrint { void print() {…} … }; class list implements hasASize { void size(…) {…}…} class figure implements hasASize, hasAPrint { void size(…) {…}; void print(…) {…} …} hasAPrint anobj; anobj.print(); What’s the problem here?

Implementing invokeinterface

• Problem: print method may have different offsets in each class implementing hasAPrint
• Solution: – Can implement invokeinterface using Smalltalklike run-time method search – (better idea) Have v-tables for interfaces that are hashed using (class, interface name) pair.

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *