Beruflich Dokumente
Kultur Dokumente
Anabel Paredes
Alexander Pinto
Victor Reyna
David Roque
Polymorphic subprogram
if no method’s parameter profile matches the number and types of the actual
parameters in a method call, but two or more methods have parameter profiles that
can be matched through coercions, which method should be called?
the designer decide how to rank all of the different coercions, so that the
compiler can choose the method that “best” matches the call.
Subtype polymorphism
- Run-time Polymorphism
- A variable of type T can access any object of type T or any type derived
from T.
C++
- Compile-Time Polymorphism
- Parametric polymorphism is provided by a subprogram that takes generic
parameters that are used in type expressions that describe the types of
the parameters of the subprogram. Different instantiations of such
subprograms can be given different generic parameters, producing
subprograms that take different types of parameters.
- Parametrically polymorphic subprograms are often called generic
subprograms.
Java
C++
Dynamic binding (dispatch)
- Abstract data types, inheritance.
- Kind of polymorphism provided by the dynamic binding of messages to
method definitions.
Problems and purpose.
- Run-time system must determine, during execution, which method
should be called, A’s or B’s?,
- Determining which type object is currently referenced by the reference.
- One purpose of dynamic binding is to allow software systems to be more
easily extended during both development and maintenance. (Car Catalog
example).
Abstract (base) Class.
● The design of an inheritance hierarchy results in a class that are so high in
the hierarchy that an instantiation would not make sense.
● It probably would not make sense to have an implemented a method in
base class. But because all of its descendant classes should have such an
implemented method, the protocol (but not the body) of that method is
included in Base. (Pure virtual method in C++).
● A class that includes at least one abstract method is called an abstract
class (abstract base class in C++). Such a class usually cannot be
instantiated, because some of its methods are declared but are not
defined (they do not have bodies).
C++
Objective-C
● There is no way to
prevent overriding of
an inherited method.
● Support for
polymorphism with its
id data type is
overkill, for it allows
variables to reference
any object, rather
than just those in an
inheritance line.
Java
In C++, a method must be defined as virtual to allow dynamic binding. In Java,
all method calls are dynamically bound unless the called method has been
defined as final, in which case it cannot be overridden and all bindings are
static. Static binding is also used if the method is static or private, both of
Advantage:
Disadvantage:
- In this purest model of object-oriented computation, all types are classes. There is no
distinction between predefined and user-defined classes.
Alternativas:
Principle of substitution
Si un lenguaje permite programas en los que una variable de una clase puede
ser sustituida por una variable de una de sus clases ancestrales, sin presentar
problemas comos los siguientes:
- Errores tipográficos
- Cambios en el comportamiento del programa.
Subtype
Does the language allow multiple inheritance (in addition to single inheritance)?
- Complexity
Information
hiding
Motivation
- CIR is static
- Built at compile time
- Template for the creation of the data of class instances
Dynamic Binding of Method Calls to
Methods
Methods that will be dynamically bound must have entries in this CIR