Download presentation
Presentation is loading. Please wait.
Published byNickolas Burns Modified over 9 years ago
1
© 2006 Pearson Addison-Wesley. All rights reserved 9.1.1 Inheritance Systems Merchandise ElectronicsClothing TelevisionCamcorderShirtDressShoe DigitalAnalog
2
© 2006 Pearson Addison-Wesley. All rights reserved 9.1.2 Another Hierarchy AcademicDisciplines Mathematics Engineering AlgebraGeometryIndustrialElectricalCivilProbability
3
© 2006 Pearson Addison-Wesley. All rights reserved 9.1.3 Ancestors & Descendants ClosedFigure Ellipse Polygon Circle Square QuadrilateralPentagonTriangle
4
© 2006 Pearson Addison-Wesley. All rights reserved 9.1.4 Conformance ClosedFigure Ellipse Polygon Circle Square QuadrilateralPentagonTriangle A type conforms to itself or any ancestor. A conforms to B means A B Conformance is _____________!
5
© 2006 Pearson Addison-Wesley. All rights reserved 9.1.5 Rule for Using Conformance An expression is permitted anywhere that its type conforms. assignment object = expression; expression must conform to object parameter passage object. method(arg1, arg2, … argN); arg1 must conform to parm1 arg2 must conform to parm2... argN must conform to parmN expressions expression Anything with type that conforms to expression can be substituted in its place. Remember: Conformance is directional! public void method(parm1, parm2,…parmN) { … } public void method(parm1, parm2,…parmN) { … }
6
© 2006 Pearson Addison-Wesley. All rights reserved 9.1.6 OvalRectangleLine JComponent Inheritance java.awt. Component «queries» + Color getBackground() + Color getForeground() + int getX() + int getY() + int getWidth() + int getHeight() + Container getParent() «update» + void setBackground( Color ) + void setForeground( Color ) + void setBounds(int, int, int, int) + void setSize( int, int ) + void setLocation( int, int ) + void paint( Graphics ) + void repaint() javax.swing. JComponent «constructor» + JComponent() «queries» + int getX() + int getY() + int getWidth() + int getHeight() «update» + void paint( Graphics ) + void paintChildren() + void setBackground( Color ) + void setForeground( Color ) java.awt. Container «update» + void add( Component ) + void add( Component, int ) + void remove( Component ) + void removeAll( )
7
© 2006 Pearson Addison-Wesley. All rights reserved 9.1.7 Using Inheritance and Conformance Write a function that returns true exactly when its Oval parameter has a height of 10 or greater. public boolean isTenOrMoreHigh( Component v ) { return v.getHeight() >= 10; } What is the advantage of using a parameter of type Component, instead of an Oval ?
8
© 2006 Pearson Addison-Wesley. All rights reserved 9.1.8 Testing for Type conformance Java provides the following operator to test for type conformance. instanceof Syntax expression instanceof className Semantics This is a Boolean expression that evaluates to true exactly when the type of expression conforms to className. Example if (myVar instanceof Component)...
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.