Inheritance Techniques

Download Report

Transcript Inheritance Techniques

Inheritance Techniques
Subcontracting
Anchored Types
ceg860 (Prasad)
L17IT
1
Inheritance is subcontracting.
C
S
T
class C { ...
void p(S s) {
…; s.r(); …
}
}
Call:
C c;
T t;
c.p(t);
ceg860 (Prasad)
class S { …
void
r() { … };
}
class T extends S { …
void
r() { … };
}
Spec: pre-post conditions
L17IT
2
• Parent Invariant Rule
• The invariants of all the parents (and therefore, the
ancestors) of a class apply to the class itself.
• Assertion Redeclaration Rule
• A routine redeclaration may only replace the
original precondition by one equal or weaker, and
the original postcondition by one equal or stronger.
{
P }
S.r()
{
Q }
{
P*}
T.r()
{
Q*}
P
=>
P*
and
Q* => Q
(Cf. Rule of consequence)
• The postcondition associated with a 0-ary routine
redeclared as an attribute manifests itself as an
additional class invariant.
ceg860 (Prasad)
L17IT
3
Odds and Ends
• Global Inheritance Structure
• Java has a tree-structured class hierarchy (with
null-type treated separately).
• The features of universal interest (such as equal(_),
clone(), toString(), “locks for concurrency”,
etc) are in class java.lang.Object.
• Frozen Features
• In Java, final can be applied to a class, a method,
a field, etc to prohibit extension, redefinition,
change, etc respectively. (This may be for security
reasons, for freezing the semantics, for defining a
constant, respectively.)
ceg860 (Prasad)
L17IT
4
Anchored Declarations : Covariance
class Device feature
alternate: Device;
set_alternate(a:Device)
is do
alternate := a
end
end
class
Usually, redeclaration
uses same signature to
introduce or substitute a
new algorithm. Eiffel also
supports redeclaration by
type specialization.
Printer inherit
Device
feature
alternate: Printer;
set_alternate(a:Printer)
is do
alternate := a
end
end
ceg860 (Prasad)
class Device feature
alternate: like Current;
set_alternate
(a:like Current)
is do
alternate := a
end
end
L17IT
5
Link
BiLink
Type Redeclaration Rule
A redeclaration of a
feature may replace the
type of the feature, or the
type of a formal, by any
type that conforms to the
original.
This is a syntactic and
purely static rule, with no
effect on run-time objects.
ceg860 (Prasad)
class Link[G] feature
item : G;
right : Link[G];
put_right(n:Link[G])
is do … end;
...
end;
class BiLink[G] feature
item : G;
left, right : like Current;
put_right(n: like left)
is do … end;
put_left(n: like left)
is do … end;
...
end;
L17IT
6
class Point {
int x,y;
boolean eq(Point p) {
return ( x == p.x)
&& ( y == p.y) ;
}}
class ColoredPoint
extends Point {
Color c;
boolean eq(Point p) {
return ( x == p.x)
&& ( y == p.y)
&& ... ;
}} // unsatisfactory
ceg860 (Prasad)
class ColoredPoint
extends Point {
Color c;
boolean eq(ColoredPoint p) {
return ( super(p) )
&& ( c == p.c ) ;
}}
//
unsatisfactory override
class ColoredPoint
extends Point {
Color c;
boolean eq(Point p) {
if (p instanceOf
ColoredPoint)
return ( super(p) )
&& ( c == p.c ) ;
else return false;
}}
L17IT
// redeclare
7
Static Typing
• Type violation:
x.f(args)
There is no f applicable to object associated
with x, or args is not “acceptable”.
• A language is statically typed if it is equipped
with consistency rules, enforceable by a compiler,
whose observance by the software text guarantees
that no execution of the system can cause a type
violation. (E.g., Eiffel, Ada, ML, etc.)
• A language is strongly typed if it can guarantee the
absence of type violations. (E.g., Java, Smalltalk, etc.)
ceg860 (Prasad)
L17IT
8
• “Nature of the beast”: Trying to guarantee
that no computation will ever fail forces
one to disallow computations that might
succeed. (E.g, n : integer = 2.0; is illegal in Ada.)
• Benefits : Reliability, Readability, Efficiency
• Typing vs Binding
* Typing: When do we know for sure that at run-time
there will be an operation corresponding to f and
applicable to the object attached to x (with the
argument arg).
» Polymorphism
* Binding: Which operation will the call execute?
» Redeclaration
ceg860 (Prasad)
L17IT
9
Typing problems
Interaction with
polymorphism
(Covariance)
Interaction between
redeclaration and
descendant hiding
Device d = new
CD-Drive();
Printer p = new
Printer();
d.set_alternate(p);
Java prohibits method
redeclaration that reduce
visibility (e.g., from
public to private). O/w,
it can always be “beaten”
by promoting subclass
object and using dynamic
binding.
Anchored declaration
does not prevent type
violation, but Java
encoding seems to work.
ceg860 (Prasad)
L17IT
10