S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Inheritance Semantics and Method Lookup.

Slides:



Advertisements
Similar presentations
1 Object Orientation James Brucker. 2 Smalltalk  Ahead of its time: consistent design and rich library.  Dynamic (like Lisp): variables have no specified.
Advertisements

Basics of Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 © Mitchell Wand, This work is licensed under a Creative Commons.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
S.Ducasse 1 Metaclasses in 7 Steps Classes are objects too... Classes are instances of other classes... One model applied twice.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
More about inheritance Exploring polymorphism. 02/12/2004Lecture 8: More about inheritance2 Main concepts to be covered method polymorphism static and.
Stéphane Ducasse 1 The Taste of Smalltalk.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
Some Basic Points on Classes
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Chapter 10 Classes Continued
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
S.Ducasse Stéphane Ducasse 1 Classes and Metaclasses - an Analysis.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
S.Ducasse Stéphane Ducasse 1 Design Heuristics.
Stéphane Ducasse 1 Visitor.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Types in programming languages1 What are types, and why do we need them?
Stéphane Ducasse 1 Elements of Design - Inheritance/Compositio n.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Dynamic Binding Object-Oriented Programming Spring
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Stéphane Ducasse 1 Singleton.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
S.Ducasse Stéphane Ducasse 1 Common Mistakes and Debugging in VW.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Stéphane Ducasse 1 Some Advanced Points on Classes.
Stéphane Ducasse7.1 Let’s Play Objects Simulate a LAN physically Set up a context for –future chapters –Exercises Some forward references to intriguate.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Stéphane Ducasse 1 Some Points on Classes.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
Stéphane Ducasse 1 Elements of Design - Unit of Reuse.
Stéphane Ducasse 1 Abstract Classes.
S.Ducasse Stéphane Ducasse 1 Some Advanced Points on Classes.
Smalltalk Meta-Programming Programming Languages HUJI 2010 Yardena Meymann.
S.Ducasse 1 Instance Initialization How to ensure that an instance is well initialized? Automatic initialize Lazy initialization Proposing the right interface.
Let’s Play Objects.
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Design Points - Law of Demeter
Inheritance and Polymorphism
Object Oriented Programming in Java
Types of Programming Languages
ATS Application Programming: Java Programming
Chapter 9 Inheritance and Polymorphism
Inheritance Basics Programming with Inheritance
Computer Programming with JAVA
Java Inheritance.
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Inheritance Semantics and Method Lookup

S.Ducasse 2 Goal Inheritance Method lookup Self/super difference

S.Ducasse 3 Inheritance Do not want to rewrite everything! Often we want small changes We would like to reuse and extend existing behavior Solution: class inheritance Each class defines or refines the definition of its ancestors

S.Ducasse 4 Inheritance New classes Can add state and behavior: color, borderColor, borderWidth, totalArea Can specialize ancestor behavior intersect: Can use ancestor’s behavior and state Can redefine ancestor’s behavior area to return totalArea

S.Ducasse 5 Inheritance in Smalltalk Single inheritance Static for the instance variables At class creation time the instance variables are collected from the superclasses and the class. No repetition of instance variables. Dynamic for the methods Late binding (all virtual) methods are looked up at run-time depending on the dynamic type of the receiver.

S.Ducasse 6 Message Sending receiver selector args Sending a message = looking up the method that should be executed and executing it Looking up a method: When a message (receiver selector args) is sent, the method corresponding to the message selector is looked up through the inheritance chain.

S.Ducasse 7 Method Lookup Two steps process The lookup starts in the CLASS of the RECEIVER. If the method is defined in the method dictionary, it is returned. Otherwise the search continues in the superclasses of the receiver's class. If no method is found and there is no superclass to explore (class Object), this is an ERROR

S.Ducasse 8 Lookup: class and inheritance

S.Ducasse 9 Some Cases

S.Ducasse 10 Method Lookup starts in Receiver Class aB foo (1) aB class => B (2) Is foo defined in B? (3) Foo is executed -> 50 aB bar (1) aB class => B (2) Is bar defined in B? (3) Is bar defined in A? (4) bar executed (5) Self class => B (6) Is foo defined in B (7) Foo is executed -> 50

S.Ducasse 11 self **always** represents the receiver A new foo -> 10 B new foo -> 10 C new foo -> 50 A new bar -> 10 B new bar -> 10 C new bar -> 50

S.Ducasse 12 When message is not found If no method is found and there is no superclass to explore (class Object), a new method called #doesNotUnderstand: is sent to the receiver, with a representation of the initial message.

S.Ducasse 13 Graphically… 1 2

S.Ducasse 14 …in Smalltalk node1 print: aPacket – node is an instance of Node – print: is looked up in the class Node – print: is not defined in Node > lookup continues in Object – print: is not defined in Object => lookup stops + exception – message: node1 doesNotUnderstand: #(#print aPacket) is executed – node1 is an instance of Node so doesNotUnderstand: is looked up in the class Node – doesNotUnderstand: is not defined in Node => lookup continues in Object – doesNotUnderstand: is defined in Object => lookup stops + method executed (open a dialog box) 14

S.Ducasse 15 Graphically…

S.Ducasse 16 Roadmap Inheritance Method lookup Self/super difference

S.Ducasse 17 How to Invoke Overridden Methods? Solution: Send messages to super When a packet is not addressed to a workstation, we just want to pass the packet to the next node, i.e., we want to perform the default behavior defined by Node. Workstation>>accept: aPacket (aPacket isAddressedTo: self) ifTrue:[Transcript show: 'Packet accepted by the Workstation ', self name asString] ifFalse: [super accept: aPacket] Design Hint: Do not send messages to super with different selectors than the original one. It introduces implicit dependency between methods with different names.

S.Ducasse 18 The semantics of super Like self, super is a pseudo-variable that refers to the receiver of the message. It is used to invoke overridden methods. When using self, the lookup of the method begins in the class of the receiver. When using super, the lookup of the method begins in the superclass of the class of the method containing the super expression

S.Ducasse 19 super changes lookup starting class A new bar -> 10 B new bar -> C new bar ->

S.Ducasse 20 super is NOT the superclass of the receiver class Suppose the WRONG hypothesis: “The semantics of super is to start the lookup of a method in the superclass of the receiver class”

S.Ducasse 21 super is NOT the superclass of the receiver class mac is instance of ColoredWorkStation Lookup starts in ColoredWorkStation Not found so goes up... accept: is defined in Workstation lookup stops method accept: is executed Workstation>>accept: does a super send Our hypothesis: start in the super of the class of the receiver => superclass of class of a ColoredWorkstation is... Workstation ! Therefore we look in workstation again!!!

S.Ducasse 22 What you should know Inheritance of instance variables is made at class definition time. Inheritance of behavior is dynamic. self **always** represents the receiver. Method lookup starts in the class of the receiver. super represents the receiver but method lookup starts in the superclass of the class using it. Self is dynamic vs. super is static.