Class Hierarchies and Type Conformance Lecture 9.1 Class Hierarchies and Type Conformance
Inheritance Systems Merchandise Electronics Clothing Television Camcorder Shirt Dress Shoe Digital Analog © 2006 Pearson Addison-Wesley. All rights reserved
Another Hierarchy AcademicDisciplines Mathematics Engineering Algebra Geometry Industrial Electrical Civil Probability © 2006 Pearson Addison-Wesley. All rights reserved
Ancestors & Descendants ClosedFigure Ellipse Polygon Circle Square Quadrilateral Pentagon Triangle Ancestors Descendants © 2006 Pearson Addison-Wesley. All rights reserved
Conformance ClosedFigure Ellipse Polygon Circle Square Quadrilateral Pentagon Triangle A type conforms to itself or any ancestor. A conforms to B means A is_a B Conformance is directional! © 2006 Pearson Addison-Wesley. All rights reserved
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 public void method(parm1, parm2,…parmN) { … } expressions expression Anything with type that conforms to expression can be substituted in its place. Remember: Conformance is directional! © 2006 Pearson Addison-Wesley. All rights reserved
javax.swing.JComponent 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() java.awt.Container «update» + void add( Component ) + void add( Component, int ) + void remove( Component ) + void removeAll( ) 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 ) Component -- note getParent returns the object to which the Component has been added or null. The add method with no second container adds UNDERNEATH existing children. removeAll removes all children. JComponent Inheritance © 2006 Pearson Addison-Wesley. All rights reserved Oval Rectangle Line
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? © 2006 Pearson Addison-Wesley. All rights reserved
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) ... © 2006 Pearson Addison-Wesley. All rights reserved