CSCE145 F2026 07 Inheritance and Polymorphism Part 02
Watch on YouTube →
Overview
Java inheritance and polymorphism let a general type represent many specific implementations: a `Shape` interface can refer to basic shapes, rectangles, triangles, and their variants, while overridden methods run according to each object’s runtime type. The lecture builds a console-drawing example to show how interfaces, class inheritance, and dynamic binding make the design extensible without changing code that processes a collection of shapes.
Key takeaways
- A Java reference typed as `Shape` can hold any implementing class, so one collection can mix basic shapes, rectangles, and triangles.
- Dynamic binding chooses an overridden method from the object's runtime class, including when an inherited method such as `drawShapeAt` calls `drawShape`.
- Interfaces establish a common contract: `Rectangle` extends `Shape`, adding width and length methods while inheriting the shape requirements.
- `BasicRectangle` reuses spacing behavior by extending `BasicShape`, then overrides `drawShape` to render a filled rectangle.
- Adding hollow or checkered rectangles and new triangle variants requires specialized subclasses, not changes to the tester's loop over `Shape[]`.
- Java allows a class to extend one superclass and implement multiple interfaces; interface inheritance uses `extends`, while class-to-interface relationships use `implements`.
Chapters
- A `Person[]` can hold `Person`, `Student`, and `Undergraduate` objects because each subclass is also a `Person`.
- `System.out.println` calls `toString`; dynamic binding selects the implementation for the object's runtime class.
- The output grows by class: `Person` prints a name, `Student` adds an ID, and `Undergraduate` adds a level.
- Overridden methods can call `super.toString()` to build on the superclass representation.
- An interface defines a type and method signatures, but the lecture's examples give it no method bodies or instance variables.
- A class uses `implements` to promise that it will provide every method required by the interface.
- An interface cannot be instantiated with `new`; a variable of interface type can reference an instance of an implementing class.
- The interface file and its public interface identifier must have matching names.
- The `Shape` interface requires `setHSpace`, `getHSpace`, `drawShape`, and `drawShapeAt`.
- Horizontal spacing and line-number offsets let each shape be positioned in the console.
- A UML dashed line with a stick arrow denotes that `BasicShape` implements `Shape`.
- The interface captures shared capabilities while leaving each shape class free to draw differently.
- `BasicShape` implements all four `Shape` methods and stores horizontal spacing in a private integer field.
- Its setter rejects negative spacing by restoring the default value of zero; its initial drawing implementation prints an asterisk.
- A static `skipSpaces` helper prints horizontal spaces, while `drawShapeAt` skips lines and then calls `drawShape`.
- A `Shape[]` can store `BasicShape` instances, allowing the tester to call drawing methods through the interface type.
- The `Rectangle` interface extends `Shape` and adds `getWidth`, `setWidth`, `getLength`, and `setLength` requirements.
- `BasicRectangle` extends `BasicShape` and implements `Rectangle`, inheriting the spacing and positioning methods.
- Its constructors initialize length and width to one, or accept horizontal spacing, length, and width as parameters.
- Length and width setters accept values of at least one and use one as the fallback for invalid values.
- `BasicRectangle.drawShape` overrides the point-drawing method to print a filled rectangle using nested loops over length and width.
- The same `Shape[]` stores both basic shapes and rectangles, and each object's own `drawShape` implementation is selected at runtime.
- `drawShapeAt` is inherited from `BasicShape`, but its call to `drawShape` dynamically dispatches to the rectangle override when appropriate.
- Java classes can extend only one class but can implement multiple interfaces; the `extends` clause comes before `implements`.
- `HollowRectangle` and `CheckeredRectangle` extend `BasicRectangle` and customize the drawing behavior by overriding `drawShape`.
- The checkered rectangle alternates asterisks and spaces according to whether the sum of its row and column indices is even or odd.
- A `Triangle` interface extends `Shape` with height accessors, and `BasicTriangle` implements it while extending `BasicShape`.
- Hollow and upside-down triangles extend `BasicTriangle`, adding new drawing forms without changing the tester's shared collection.
- Classes extend classes, interfaces extend interfaces, and classes implement interfaces.
- A class may implement several interfaces, listed with commas, but may extend only one class.
- New types such as circles or rhombuses can join the shape system by implementing the required interface methods.
- The `Shape[]` client code remains unchanged as new shape classes and drawing styles are added.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofSC_CSCE145_CSCE146.