1 / 11

Inheritance

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 {

samira
Download Presentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. 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

  4. 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(); } }

  5. 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” ); }

  6. 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(); } ... }

More Related