110 likes | 252 Views
Inheritance. When one class inherits another class, the new class becomes a specialized version of the original. Java Syntax. public class subclass extends superclass { . . . }. UML (class diagram) Notation. public class RedDot extends Oval {
E N D
Inheritance • When one class inherits another class, the new class becomes a specialized version of the original. Java Syntax public classsubclassextendssuperclass { . . . } UML (class diagram) Notation
public class RedDot extends Oval { public RedDot( int x, int y, int d ) { super(x, y, d, d); setBackground( Color. red ); } } Client code RedDot dotty; dotty = new RedDot(10, 20, 5); theWindow.add(dotty, 0); dotty.repaint(); // methods such as setSize and setLocation // are also available on dotty.
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 ) Line Rectangle Oval JComponent Inheritance
import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame win; private HappyFaceleftFace, rightFace; public Driver() { win = new JFrame("Smiles"); win.setBounds(20, 20, 600, 400); win.getContentPane().setBackground(Color.black); win.setLayout(null); win.setVisible(true); leftFace = new HappyFace(20, 100, 100); win.add(leftFace); rightFace = new HappyFace(380, 100, 200); win.add(rightFace); win.repaint(); } public static void main(String[] args) { new Driver(); } }
Equality There are two different kinds of equality for reference data. Identity equality means that two expressions have the same identity. (i.e., the two expressions represent the same object.) Content equality means that two expressions represent objects with the same value/content . The == symbol tests for identity equality, when applied to reference data. Example Oval ov1, ov2; ov1 = new Oval(0, 0, 100, 100); ov2 = new Oval(0, 0, 100, 100); if (ov1 == ov2) { System.out.println( “identity equality” ); } else { System.out.println( “not identity equality” ); }
Content Equality The equals method from Object provides a potential content equality check. Example public class MyOval extends Oval { ... public boolean equals(Object z) { return (z instanceof Oval) && ((Oval)z).getX() == getX() && ((Oval)z).getY() == getY() && ((Oval)z).getWidth() == getWidth() && ((Oval)z).getHeight() == getHeight() && ((Oval)z).getBackground() == getBackground() && ((Oval)z).getParent() == getParent(); } ... }