2008 Sccc Inheritance
description
Transcript of 2008 Sccc Inheritance
![Page 2: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/2.jpg)
• To introduce research problems related to class-inheritance
• Present 2 state-of-the-art research topics related to class inheritance
Goal of this lecture
![Page 3: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/3.jpg)
Sources & references
• Wirfs-Brock & McKean, Object Design — Roles, Responsibilities and Collaborations, 2003.
• Alexandre Bergel, Stéphane Ducasse, and Oscar Nierstrasz, Classbox/J: Controlling the Scope of Change in Java, OOPSLA'05
• Damien Cassou, Stéphane Ducasse and Roel Wuyts, Traits at Work: the design of a new trait-based stream library, In Journal of Computer Languages, Systems and Structures, Elsevier, 2008
• Stephane Ducasse, Roel Wuts, Alexandre Bergel, and Oscar Nierstrasz, User-Changeable Visibility: Resolving Unanticipated Name Clashes in Traits, OOPSLA'07
![Page 4: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/4.jpg)
Outline
1. Inheritance
2. Classboxes: evolving behavior
3. Traits: composing behavior
4. Concluding words
![Page 5: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/5.jpg)
Inheritance
• Inheritance in object-oriented programming languages is a mechanism to:
- derive new subclasses from existing classes
- where subclasses inherit all the features from their parent(s)
- and may selectively override the implementation of some features.
![Page 6: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/6.jpg)
Inheritance mechanisms
• OO languages realize inheritance in different ways:
- self: dynamically access subclass methods
- super: statically access overridden, inherited methods
- multiple inheritance: inherit features from multiple superclasses
- abstract classes: partially defined classes (to inherit from only)
- mixins: build classes from partial sets of features
- interfaces: specify method argument and return types
- subtyping: guarantees that subclass instances can be substituted
![Page 7: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/7.jpg)
Classboxes for evolution
I. Problem: AWT and Swing anomalies
II. Model: Classbox/J
III. Solution: Swing as a classbox
IV. Ongoing work: general scoping mechanism (not presented)
![Page 8: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/8.jpg)
Presentation of AWT
• In the AWT framework:
- Widgets are components (i.e., inherit from Component)
- A frame is a window (Frame is a subclass of Window)
java.awt
Component
ButtonContainerWindowFrame
![Page 9: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/9.jpg)
Broken Inheritance in Swing
javax.swing
java.awt
Component
ButtonContainerWindowFrame
JButton
JComponentJWindowJFrame
![Page 10: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/10.jpg)
Problem: Code Duplicationjava.awt
Component
ButtonContainerWindowFrame
javax.swing
JButtonaccessibleContextrootPaneupdate()setLayout()...
accessibleContextrootPaneupdate()setLayout()...
accessibleContextupdate()
Code Duplication
JComponentJFrame JWindow
![Page 11: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/11.jpg)
Problem: explicit type operationpublic class Container extends Component { Component components[] = new Component [0]; public Component add (Component comp) {...}}
public class JComponent extends Container { public void paintChildren (Graphics g) { for (; i>=0 ; i--) { Component comp = getComponent (i); isJComponent = (comp instanceof JComponent); ... ((JComponent) comp).getBounds(); } }}
![Page 12: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/12.jpg)
Alternative to inheritance
• AWT couldn’t be enhanced without risk of breaking existing code.
• Swing is, therefore, built on the top of AWT using subclassing.
• As a result, Swing is a big mess internally!
• We need an alternative to inheritance to support unanticipated changes.
![Page 13: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/13.jpg)
Classbox/J
• Module system for Java allowing classes to be refined without breaking former clients.
• A classbox is like a package where:
- a class defined or imported within a classbox p can be imported by another classbox (transitive import).
- class members can be added or redefined on an imported class with the keyword refine.
- a refined method can access its original behavior using the original keyword
![Page 14: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/14.jpg)
Swing Refactored as a Classbox
javax.swing
AwtCB
Component
ContainerWindowFrame
Button
JButtonaccessibleContextrootPaneupdate()setLayout()...
accessibleContextrootPaneupdate()setLayout()...
accessibleContextextupdate()
JComponentJFrame JWindow
![Page 15: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/15.jpg)
Swing Refactored as a Classbox
SwingCB
AwtCB
Component
ContainerWindow
Component
Button
Frame WindowrootPane
setLayout()setRootPane()setContentPane()...
accessibleContextcomponent
update()add(Component)remove(Component)
FrameButton
![Page 16: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/16.jpg)
Swing Refactoring
• 6500 lines of code refactored over 4 classes.
• Inheritance defined in AwtCB is fully preserved in SwingCB:
- In SwingCB, every widget is a component (i.e., inherits from the extended AWT Component).
- The property “a frame is a window” is true in SwingCB.
• Removed duplicated code: the refined Frame is 29 % smaller than the original JFrame.
• Explicit type checks like obj instanceof JComponent and (JComponent)obj are avoided.
![Page 17: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/17.jpg)
Properties of Classboxes
• Minimal extension of the Java syntax (transitive import, refine and original keywords).
• Refinements are confined to the classbox that define them and to classboxes that import refined classes.
• Method redefinitions have precedence over previous definitions.
• Classes can be refined without risk of breaking former clients.
![Page 18: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/18.jpg)
Traits
I. Problem: Stream in Squeak anomalies
II. Model: Traits
III. Solution: Refactoring with Traits
IV. Ongoing work: Pure trait language (not presented)
![Page 19: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/19.jpg)
Stream in Squeak
• Example of a library that has been in use for almost 20 years
• Contains many flaws in its conception
![Page 20: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/20.jpg)
Stream in Squeak
atEndclosecontentsdo:flushnextnext:next:put:nextPut:nextPutAll:upToEnd
Stream
atEndcontentsisEmptynext:peekpositionposition:resetsetToEndskip:skipTo:upTo:upToEnd
collectionpositionreadLimit
Positionable
Stream
nextnext:nextPut:sizeupTo:upToEnd
ReadStream
contentsflushnextnextPut:nextPutAll:position:resetsetToEndsizespacecr
writeLimitWriteStream
closecontentsnextnext:
ReadWriteStream
atEndcloseflushfullNamenextnext:nextPut:nextPutAll:peekpositionposition:resetsetToEndsizeskip:upTo:upToEnd
rwmodenamefileIDbuffer1
FileStream
![Page 21: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/21.jpg)
Methods too high
atEndclosecontentsdo:flushnextnext:next:put:nextPut:nextPutAll:upToEnd
Stream
atEndcontentsisEmptynext:peekpositionposition:resetsetToEndskip:skipTo:upTo:upToEnd
collectionpositionreadLimit
Positionable
Stream
nextnext:nextPut:sizeupTo:upToEnd
ReadStream
contentsflushnextnextPut:nextPutAll:position:resetsetToEndsizespacecr
writeLimitWriteStream
closecontentsnextnext:
ReadWriteStream
atEndcloseflushfullNamenextnext:nextPut:nextPutAll:peekpositionposition:resetsetToEndsizeskip:upTo:upToEnd
rwmodenamefileIDbuffer1
FileStream
nextPutAll: aCollection aCollection do: [:v| self nextPut: v]. ^ aCollection
![Page 22: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/22.jpg)
Methods too high
• The nextPut: method defined in Stream allows for element addition
• The ReadStream class is read-only
• It therefore needs to “cancel” this method by redefining it and throwing an exception
![Page 23: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/23.jpg)
Unused state
atEndclosecontentsdo:flushnextnext:next:put:nextPut:nextPutAll:upToEnd
Stream
atEndcontentsisEmptynext:peekpositionposition:resetsetToEndskip:skipTo:upTo:upToEnd
collectionpositionreadLimit
Positionable
Stream
nextnext:nextPut:sizeupTo:upToEnd
ReadStream
contentsflushnextnextPut:nextPutAll:position:resetsetToEndsizespacecr
writeLimitWriteStream
closecontentsnextnext:
ReadWriteStream
atEndcloseflushfullNamenextnext:nextPut:nextPutAll:peekpositionposition:resetsetToEndsizeskip:upTo:upToEnd
rwmodenamefileIDbuffer1
FileStream
![Page 24: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/24.jpg)
Unused state
• State defined in the super classes are becoming irrelevant in subclasses
• FileStream does not use inherited variables
![Page 25: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/25.jpg)
Multiple inheritance simulation
atEndclosecontentsdo:flushnextnext:next:put:nextPut:nextPutAll:upToEnd
Stream
atEndcontentsisEmptynext:peekpositionposition:resetsetToEndskip:skipTo:upTo:upToEnd
collectionpositionreadLimit
Positionable
Stream
nextnext:nextPut:sizeupTo:upToEnd
ReadStream
contentsflushnextnextPut:nextPutAll:position:resetsetToEndsizespacecr
writeLimitWriteStream
closecontentsnextnext:
ReadWriteStream
atEndcloseflushfullNamenextnext:nextPut:nextPutAll:peekpositionposition:resetsetToEndsizeskip:upTo:upToEnd
rwmodenamefileIDbuffer1
FileStream
![Page 26: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/26.jpg)
Multiple inheritance
• Methods are duplicated among different class hierarchies
![Page 27: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/27.jpg)
Class responsibilities
• Too many responsibilities for classes
- object factories
- group methods when subclassing `
![Page 28: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/28.jpg)
Class schizophrenia?
• Too many responsibilities for classes
- object factories => need for completeness
- group methods when subclassing => need to incorporate incomplete fragments
![Page 29: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/29.jpg)
Traits
TCircleareaboundsdiameterhash...
radiusradius:centercenter:
• Traits are parameterized behaviors- traits provide a set of methods - traits require a set of methods - traits are purely behavioral
![Page 30: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/30.jpg)
Class = Superclass + State + Traits + Glue Methods
• Traits are the behavioral building blocks of classes
Generic propertiesObject
Component Geometrical
RectangleWidget
setX1(...)setY1(...)
RectangleShape
setX1(...)setY1(...)TRectangle
TColorx1, y1, x2, y2 point1, point2
TRectangle
TColor
...
![Page 31: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/31.jpg)
Composition rules
Class methods take precedence over trait methods
ColoredCircle
TColorhuergb
draw
ColoredCircle
TColorhuergb
TCircledrawradiusdrawdraw
TCircledrawradius
![Page 32: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/32.jpg)
Conflicts are explicitly resolved
• Override the conflict with a glue method
- Aliases provide access to the conflicting methods
• Avoid the conflict
- Exclude the conflicting method from one trait
ColoredCircle
TColorhash
TCirclehashhash -> circleHash
hash
ColoredCircle
TColorhash
TCirclehashhash hash -> circleHash
hash -> colorHash
![Page 33: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/33.jpg)
Flattening property
ColoredCircle
TColor
TCircledraw
rgb
draw
hue
radius
Object
ColoredCircle
Object
rgbhue
radius
draw
equivalentStructured view Flat view
![Page 34: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/34.jpg)
Stream revisited
do:nextMatchFor:next:peekFor:skip:skipTo:upTo:upToEndupToElementSatisfying:
atEndnextpeekoutputCollectionClass
TGettableStream
atEndatStartbackcloseisEmptyposition:resetsetToEnd
positionsetPosition:size
TPositionableStream
nextPutAll:next:put:print:flush
nextPut:TPuttableStream
Core
binarycloseclosedisBinaryisClosedisStream
TStream
backbackUpTo:match:nextDelimited:skip:
TGettablePositionableStream TPuttablePositionableStream
writeBack
@ {#basicBack->#back}
![Page 35: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/35.jpg)
Concluding words
• Hard topic where proving a better solution requires a significant effort
• Other hot topics: Virtual classes, Nested inheritance, Selector namespaces
![Page 36: 2008 Sccc Inheritance](https://reader034.fdocuments.in/reader034/viewer/2022042623/546454acaf795997368b4827/html5/thumbnails/36.jpg)