Download presentation
Presentation is loading. Please wait.
Published byJulia Preston Modified over 9 years ago
1
ceg860 (Prasad)L17IT1 Inheritance Techniques Subcontracting Anchored Types
2
ceg860 (Prasad)L17IT2 Inheritance is subcontracting. class C {... void p(S s) { …; s.r(); … } Call: C c; T t; c.p(t); class S { … void r() { … }; } class T extends S { … void r() { … }; } Spec: pre-post conditions C S T
3
ceg860 (Prasad)L17IT3 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.
4
ceg860 (Prasad)L17IT4 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.)
5
ceg860 (Prasad)L17IT5 Anchored Declarations : Covariance classfeature class Device feature alternate: Device; set_alternate(a:Device) is do alternate := a end end class class Printer inherit feature Device feature alternate: Printer; set_alternate(a:Printer) is do alternate := a end end Usually, redeclaration uses same signature to introduce or substitute a new algorithm. Eiffel also supports redeclaration by type specialization. classfeature class Device feature like Current alternate: like Current; set_alternate like Current (a:like Current) is do alternate := a end end
6
ceg860 (Prasad)L17IT6 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. classfeature class Link[G] feature item : G; right : Link[G]; put_right(n:Link[G]) is do … end;......end; classfeature class BiLink[G] feature item : G; like Current left, right : like Current; like put_right(n: like left) is do … end; like put_left(n: like left) is do … end;......end; Link BiLink
7
ceg860 (Prasad)L17IT7 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 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; }} // redeclare
8
ceg860 (Prasad)L17IT8 Static Typing Type violation: x.f(args) There is no f applicable to object associated with x, or args is not “acceptable”. statically typedA 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.) strongly typedA language is strongly typed if it can guarantee the absence of type violations. ( E.g., Java, Smalltalk, etc.)
9
ceg860 (Prasad)L17IT9 “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
10
ceg860 (Prasad)L17IT10 Typing problems Interaction with polymorphism (Covariance) Device d = new CD-Drive(); Printer p = new Printer(); d.set_alternate(p); Anchored declaration does not prevent type violation, but Java encoding seems to work. Interaction between redeclaration and descendant hiding 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.