name: inverse layout: true class: taylor msoe --- class: center, middle .title[ # More Inheritance Review ## Abstract Classes/Methods, Casting References, Visibility Modifiers, Interfaces ] ??? 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. --- # Abstract * If a class is declared abstract, cannot create objects from it -- * E.g., `public abstract Shape {` -- * A class **must** be declared abstract if it has one or more abstract methods -- * An abstract method is declared but not implemented -- * E.g., `public abstract void draw();` -- * A subclass of an abstract class must either: + be declared abstract, or + implement all inherited abstract methods --- # Motivation for Abstract Classes * Provide common functionality for subclasses (code reuse) -- * Use general reference to point to specific object type + Reveal common behavior even if the behavior varies across subclasses + E.g., all shapes can be drawn, but implementation depends on specific type --- # Casting References * Java will do an implicit type cast from subclass reference to superclass reference, e.g., ``` Shape shape = ___; Circle circle = ___; shape = circle; ``` -- + Implicit cast from `Circle` to `Shape` -- * Must explicitly cast from superclass to subclass ``` circle = (Circle)shape; ``` -- * If the `shape` reference does not refer to a `Circle` object, a `ClassCastException` will be thrown at runtime --- # Visibility Modifiers * Every attribute and method has a visibility level associated with it + `public` - All classes have access [UML: **`+`**] + `private` - Only the class has access [UML: **`-`**] -- + package - Only classes in the same package have access [UML: **`~`**] -- + `protected` - Only subclasses and classes in the same package have access [UML: **`#`**] -- ``` public int publicVisibility; // Most visible protected int protectedVisibility; int packageVisibility; private int privateVisibility; // Least visible ``` --- # Interfaces * An interface advertises functionality but doesn't provide an implementation -- * Cannot create objects from an interface -- * An interface reference can point to any object from a class that implements the interface -- * Here too, the reference determines what methods can be called -- * The implementing class **can-do** what the interface claims for functionality --- # Inteface Example * [`CharSequence`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/CharSequence.html) - a readable sequence of `char` values -- ``` public interface CharSequence { char charAt(int index); int length(); CharSequence subSequence(int start, int end); String toString(); } ``` -- * Methods are `public` by default -- * The `String` class implements the `CharSequence` interface ``` public class String implements CharSequence { ``` -- + This is a simplified declaration of the `String` class --- # Intefaces Continued... * Other classes also implement the `CharSequence` interface: `StringBuffer`, `StringBuilder`, `CharBuffer`, `Segment` -- * Whenever a `CharSequence` is needed, an object from any of the implementing classes will work -- * Summary: + Inheritance describes an **is-a** relationship + A class that implements an interface has a **can-do** relationship