19-polymorphism

19-polymorphism - Polymorphism CMSC 330: Organization of...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CMSC 330: Organization of Programming Languages Polymorphism CMSC 330 2 Polymorphism Definition • Feature that allows values of different data types to be handled using a uniform interface Applicable to • Functions Same function applied to different data types Example • Data types Same data type can contain different data types Example type ‘a option = None | Some of ‘a let hd = function (h::_) -> h CMSC 330 3 Two Kinds of Polymorphism Described by Strachey in 1967 Ad hoc polymorphism • Range of types is finite • Combinations must be specified in advance • Behavior may differ based on type of arguments Parametric polymorphism • Code written without mention of specific type • May be transparently used with arbitrary # of types • Behavior is same for different types of arguments CMSC 330 4 Polymorphism Overview Ad-hoc • Subtype (for OO languages) Sometimes not considered ad-hoc, but referred to as subtype polymorphism • Overloading Operator overloading Parametric • ML types • A.k.a. generic programming (for OO languages) Bounded parametric polymorphism combines subtype and parametric polymorphism CMSC 330 5 Subtype Polymorphism Found in object-oriented programming languages • Supported through inheritance Any function w/ object as parameter is polymorphic • If formal parameter is of class A • Argument may be any object from subclass of A class A { … } class B extends A { … } // subclass static void f(A arg) { … } A a = new A(); B b = new B(); f(a); f(b); // f accepts arg of type A or B CMSC 330 6 Liskov Substitution Principle If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T , the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T . • I.e, if anyone expecting a T can be given an S , then S is a subtype of T . Note this speaks not just to the types of methods, but their overall behavior . At least the types must match
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 CMSC 330 7 Overloading Multiple copies of function • Same function name • But different number / type of parameters Arguments determine function actually invoked • Function is uniquely identified not by function name, but by name + order & number of argument type(s) print(Integer i) print_Integer(…) print(Float f) print_Float(…) static void print(Integer arg) { … } static void print(Float arg) { … } print(1); // invokes 1 st print print(3.14); // invokes 2 nd print CMSC 330 8 Operator Overloading Treat operators as functions • With special syntax for invocations • Behavior different depending on operand type Example • + in Java 1 + 2 // integer addition 1.0 + 3.14 // float addition “Hello” + “world” // string concatenation CMSC 330 9 User-defined operators Supported in languages such as Ruby, C++ • Makes user data types appear more like native types • Not overloading; rather, non-alphabetic method names Examples • Defining function for
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2012 for the course CMSC 330 taught by Professor Staff during the Fall '08 term at Maryland.

Page1 / 7

19-polymorphism - Polymorphism CMSC 330: Organization of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online