name: inverse layout: true class: taylor msoe --- class: center, middle .title[ # Inheritance Review ] ??? Toggle speaker view by pressing P Pressing C clones the slideshow view, which is the view to put on the projector if you're using speaker view. Press ? to toggle keyboard commands help. --- # Inheritance * A relationship between classes -- * Super class - more generalized type -- + `Animal` -- * Sub classes - more specific types of the super class -- + `Dog` + `Gnat` + `Robin` -- * Sub class **is-a** super class --- # Motivation for Inheritance * Code reuse -- * Identify similar but not identical types -- * Use general reference to point to specific object type --- class: animated slideInUp center, middle * Consider [javafx.scene.control.Button](https://javadoc.taylorial.com/javafx.controls/scene/control/ButtonBase.html)
--- class: center, middle * Open triangle signifies inheritance (points to super class)
??? * See the Javadoc for the [javafx.scene.control.Button](https://javadoc.taylorial.com/javafx.controls/scene/control/ButtonBase.html) class ## Code reuse * Properties managed by `Control`: + contextMenu + skin + toolTip * Properties managed by `Labeled`: + font + graphic + lineSpacing + etc... * Properties managed by `ButtonBase`: + armed + onAction --- # Benefits of Inheritance ## Identifying similar but not identical types * `Separator`, `MenuBar`, `Labeled`, `ScrollPane`, `ListView`, and `ToolBar` are all specific types of `Control`s -- ## General ref can point to specific object type ``` Control control = new Separator(); Labeled labeledControl = new Label("I'm an enigma"); ButtonBase button = new Button("I'm not a dress shirt"); button = new CheckBox("I don't play hockey"); control = button; button = labeledControl; // <-- Illegal since a labeled control may not be a button ``` ??? ## Analogies * I can refer to my daughter as my child * I can refer to my maternal grandfather as my ancestor * I can refer to my iPhone 8 plus as my cell phone or just phone ## Jokes explained * An enigma - defies labeling * A shirt - needs to be pressed (ironed) * Hockey - I don't want to get hurt by being checked --- # Code Reuse ``` javafx.scene.control.Control javafx.scene.control.Labeled javafx.scene.control.ButtonBase javafx.scene.control.Button ``` * Functionality shared by all objects that are `Control`s is implemented in the `Control` class -- + E.g., `setToolTip()` -- * Functionality shared by all objects that are `Labeled` controls is implemented in the `Labeled` class -- + E.g., `setText()` --- # Code-Reuse Classes * It may make sense to create a super class solely to promote code reuse -- * The `ButtonBase` class exists solely for code reuse -- + All buttons (buttons, hyperlinks, checkboxes, radio buttons, etc...) need to respond to actions + `ButtonBase` manages what code should be run when the button is pressed + Classes with _Base_ or _Abstract_ in the name are typically created to promote "code reuse" --- # Synonyms * Super class ↔ Parent class * Sub class ↔ Child class * `B` extends `A` ↔ `B` derives from `A` ↔ `B` is a sub class of `A`