CSCE145 F2026 07 Inheritance and Polymorphism Part 01
Watch on YouTube →
Overview
Java inheritance lets a subclass extend a superclass through an “is-a” relationship: shared data and behavior can live in general classes, while specialized classes add their own features. The lecture builds a university personnel example from Person to Student to Undergraduate, demonstrating `extends`, `super` constructors and method calls, overriding `toString` and `equals`, and polymorphic substitution in a `Person[]`.
Key takeaways
- Use inheritance when the relationship is genuinely “is-a”: an Undergraduate is a Student, and a Student is a Person.
- A subclass constructor must initialize its superclass explicitly with `super(...)`; pass inherited fields such as a student’s name to the superclass constructor rather than duplicating them.
- Overridden `toString` and `equals` methods can preserve superclass logic by calling `super.toString()` or `super.equals(...)` and then checking subclass-specific fields.
- Polymorphism lets Java accept a subtype wherever its superclass is expected, enabling a `Person[]` to contain Person, Student, and Undergraduate objects together.
- Changes to shared superclass data or behavior can benefit every subclass, while subclasses can add specialized properties such as Student’s ID or Undergraduate’s level.
Chapters
- Inheritance creates an “is-a” relationship: a subclass is a more specific version of its superclass.
- Java uses `extends` to declare a subclass; private superclass fields are accessed through inherited accessors and mutators rather than directly.
- Subclasses do not inherit constructors, so their constructors call `super(...)` first to initialize superclass data.
- Overriding redefines a superclass method with the same signature; `super.toString()` and `super.equals(...)` reuse superclass behavior, unlike overloading with different parameters.
- The personnel system must represent undergraduates, graduate students, faculty, and staff, and support adding, removing, and viewing people.
- Undergraduates need a name, student ID, and level; graduate students need a name, ID, and advisor.
- Faculty and staff share name and salary, motivating an Employee superclass; Student and Employee can both inherit shared name data from Person.
- The UML inheritance arrows capture the design: Undergraduate and Graduate Student extend Student, while Faculty and Staff extend Employee.
- Person stores a private `String name`, validates null input in `setName`, and provides constructors, `toString`, and `equals`.
- Student extends Person and adds a private integer ID; its default constructor calls `super()` and initializes the ID to 0.
- Student’s parameterized constructor accepts both the inherited name and its own ID, passes the name to `super(name)`, and sets the ID through its mutator.
- Student’s `toString` appends the ID to `super.toString()`, while `equals` checks the superclass name comparison and matching IDs.
- Undergraduate extends Student and adds a private integer level: 1 for freshman, 2 for sophomore, 3 for junior, and 4 for senior.
- Its default constructor calls `super()` and sets level 1; its parameterized constructor passes name and ID to Student before setting level.
- The level mutator accepts only values from 1 through 4 and resets invalid values to 1.
- Undergraduate’s `toString` and `equals` build on Student’s implementations, thereby including Person’s name, Student’s ID, and the undergraduate level.
- An Undergraduate can be passed where Student is expected, and a Student where Person is expected, because each is a subtype of the next.
- Java classes ultimately extend Object, which supplies `toString` and `equals`; overriding these methods provides useful class-specific behavior instead of the default class-name-and-memory-address representation.
- Polymorphism means one superclass type can represent multiple specialized forms, with dynamic (late) binding selecting the applicable overridden behavior.
- Adding shared data such as an address or emergency contact to Person makes that property available throughout the classes that inherit from Person.
- A `Person[3]` array can hold a Person, a Student, and an Undergraduate in the same collection.
- The example assigns each array entry a different subtype, then loops through the array and prints each entry.
- Printing shows the subtype-specific fields: name for Person, name and ID for Student, and name, ID, and level for Undergraduate.
- The shared Person collection demonstrates how inheritance and polymorphism make mixed-type data easier to manage.
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.