CSCE145 F2026 07 Inheritance and Polymorphism Part 02
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.
Transcript excerpt
0:00 Hey everyone, how goes it? Welcome back to computer science 145. Today we're going to continue to discuss inheritance and polymorphism. In a previous lecture, we began discussing the concept of inheritance. Inheritance allows data and methods to be inherited or absorbed from one class into another. In Java, this occurs between two classes. The subclass, the child, and that's the class that's inheriting from the other one. and the superass the parent the class that is being inherited from. This is great for extending the properties and functionality of one class into another. The subclass becomes a more specific version of the superclass and the superass is a more general version of the subclass. This also creates what's called an is a relationship that is denoted by a block
0:49 arrow. We then started discussing this other concept that's called polymorphism. one becomes many. A superass can be extended or implemented in many different ways. A change to the superclass can be reflected across all of the subclasses. This allows substitution of one class for another as long as the class is an extension. In the previous lectures example, this is how the dot equals method worked for all of the different types. And this is all made possible by a concept called dynamic binding otherwise known as late binding. Today we're going to further explore this idea of polymorphism. Now another way to think about that is polymorphism is many forms actions or functionality. In other words, methods
1:38 can be implemented in many different ways. As we saw before that equals method and the two-string method were implemented differently for each one of the different types. This also allows changes in the subclasses methods to be applied to the inherited super classes. Looking at a part from the previous
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
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.