CSCE145 F2026 07 Inheritance and Polymorphism Part 01
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.
Transcript excerpt
0:00 Hey everyone, how goes it? And welcome back to computer science 145. Today we're going to discuss inheritance and polymorphism. In previous lectures, we've been discussing code organization. Along with this, we've been introducing these concepts called object-oriented programming. One of the major concepts of object-oriented programming that we've gone over is called encapsulation. In other words, information hiding. Now we're going to discuss the other two principles of object-oriented programming. Inheritance and polymorphism. Inheritance allows data and methods to be inherited to be absorbed from one class to another. Now in Java, this occurs between two classes. You have what's called the subclass or the child, and this is the
0:49 class that is inheriting from another. You also have the superass the parent and this is the class that is being inherited. Now in a lot of ways this does work like biological inheritance where you inherit properties from your parents. Now this concept is great for extending the properties and functionality from one class into another. The best way to think about this is the subclass becomes a more specific version of the superclass and the superclass is a more general version of the subclass. This also creates what's called an is a relationship. So the child is a more specific version
1:37 of the parent. Private properties and methods are not inherited from the superass. However, they can still be used. Instance variables from the superass have to be accessed through their accessors and also instance variables can be modified in the superclass via their mutators. Now, in order to start using inheritance, the reserved word extends is used to inherit from one class into another. So, for instance, inside of the class definition, you have public class and then the subclasses identifier. Then you have this reserved word extends followed
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.