Polymorphism in languages and its implications (Smalltalk, Modula-3, Java, and C++)
Résumé
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
Lecture recommandée Cours alignement conventionnel Cours alignement conventionnel en PDF, plus de 30 000 cours électromécanique gratuit, exercices gratuit, rapports pfe, l... Electromécanique Lire• 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.
- Format
- Domaine
- Cours JAVA/J2EE
- Publié le
- 10 janv. 2021
- Vues
- 1
Ressources liées
Programmation JAVA Sun service formation
Commentaires (0)
Connectez-vous pour laisser un commentaire.
Soyez le premier à commenter
Partagez votre avis sur ce document — questions, retours ou votre propre expérience.