UNC COMP 301 - F26/Lec 6 - Abstract classes; Inheritance application; Aggregation, Composition
Overview
Muhammad Sayeed Ghani explains how Java abstract classes enforce shared behavior when no common implementation exists, then applies interfaces, inheritance, and abstract methods to refactor four animal classes. He introduces aggregation and composition through a university Course model, distinguishing them by component ownership and lifetime while noting that real designs can fall along a spectrum.
Key takeaways
- A Java abstract class is appropriate when subclasses must provide a method but no superclass implementation makes semantic sense; for example, Person can require getStatus without inventing a universal meaning for status.
- An abstract class differs from an interface because it can carry instance state and constructors, while still enforcing method contracts and preventing direct instantiation.
- Refactoring the four animal classes separates common structure from species-specific behavior: Animal implementation holds ID and Point3D location, while subclasses implement movement and the abstract speak method.
- A final reference does not make a referenced object immutable: Course can keep a final student-list reference while its roster changes through enrollment and dropping.
- Aggregation and composition both describe has-a relationships, but differ in component ownership and lifetime; real designs, such as vehicles and their parts, may fall between the two extremes.
Chapters
- Students should accept the Assignment 2 invitation before it expires that night, even if they are not ready to begin the work.
- The lecture covers abstract classes and inheritance applications before beginning object relationships.
- The earlier Student and Professor classes shared a name field, constructor logic, and getName method, which were moved into the Person superclass.
- Student status is derived from credits, such as freshman or senior, while Professor status comes from an academic rank field.
- Because status has different meanings for students, professors, and other people, Person has no clear default getStatus behavior.
- Declaring getStatus as an abstract method requires subclasses such as Student and Professor to provide implementations.
- A Java class with at least one abstract method must itself be abstract, and an abstract class cannot be instantiated.
- Unlike an interface, an abstract class can contain fields and constructors, so it can hold state as well as define a contract.
- An abstract class supports code that can call a method such as getStatus without depending on whether the runtime object is a Student or Professor.
- Use an abstract method when all subclasses must provide behavior but the superclass has no sensible default implementation.
- A class can also be abstract without abstract methods, for example to prevent creation of a generic Animal when only specific animals should be instantiated.
- The inheritance example starts with separate BlackBear, PolarBear, Python, and Robin classes that duplicate fields and methods.
- Point3D represents an animal's location with x, y, and z coordinates, and calculates distance using the three-dimensional Pythagorean formula.
- Each animal has an ID and location, but movement and vocalization differ by species.
- Black bears and polar bears share lumbering or trekking movement and growling, but return different colors.
- A Python slithers, rejects destinations with a nonzero z-coordinate, and hisses.
- A Robin can fly to elevated or distant destinations, and otherwise walks or hops; it chirps.
- The Animal interface defines four required operations: getID, getLocation, move, and speak.
- BlackBear, PolarBear, Python, and Robin implement Animal, allowing code to work with any of the four through the common type.
- An Animal implementation superclass can hold shared ID and Point3D location fields, along with their constructor and getters.
- The species-specific move methods retain their own travel logic, such as a bear trekking or a Python slithering.
- All four move methods share the final step of updating location to the requested destination.
- Calling super.move(destination) centralizes that update in the parent class while preserving distinct subclass behavior.
- Because bears growl, pythons hiss, and robins chirp, Animal implementation has no useful shared speak body.
- Declaring speak abstract makes the Animal implementation class abstract and requires each concrete animal class to override speak.
- The BlackBear and PolarBear classes could be further refactored through a Bear interface and implementation to share bear-specific behavior.
- A line represented by two Point objects is simpler to work with than four independent coordinates, illustrating abstraction through object encapsulation.
- More complex objects can encapsulate other objects, as a Triangle can contain three points and a Course can contain a Room, Professor, and student roster.
- The lecture introduces a Room class with private fields, final values, and a useful toString representation.
- The Course class stores a Professor, Room, student roster, course name, and credits, with the fields declared private and final.
- Final fields prevent reassignment of references, but a final roster reference can still point to a List whose students are added or removed.
- As a result, the Course object is not fully immutable even though its fields are final.
- The example constructs professors, rooms, and students separately, then passes or adds them to Course objects such as COM 210.
- A Course has a Professor, Room, and students, which is a has-a relationship rather than the is-a relationship used in inheritance.
- These externally created objects can exist independently of the Course, making the example an aggregation.
- In aggregation, components are generally created outside the container and can outlive it, as books and a calculator can remain after leaving a backpack.
- In composition, the container owns components whose lifetimes depend on it, as the lecture's human-body example relates a body to its organs.
- A building that creates and provides access to its rooms illustrates composition more closely than a Course receiving an independently created Room.
- A backpack containing independently usable books and a calculator is a clear aggregation example.
- A Vehicle with wheels, axles, and an engine could be modeled as aggregation or composition depending on whether parts are independently managed or owned as integral components.
- A car dealer receiving complete cars and a repair shop replacing engines represent different ownership assumptions, so the relationship depends on the use case.
- The lecture closes by previewing a later design discussion about choosing composition or aggregation over inheritance.
Transcript excerpt
0:01 So before I begin, um just wanted to ask you folks in case you haven't accepted the invite on assignment number two, I would strongly recommend that you do so quickly. Um I've been told that about almost half of you haven't accepted that invite. Uh so the invite is due to expire by tonight. Um that's one of the reasons. Of course, we could reissue it, which we don't want to unnecessarily. However, um we also want to avoid uh folks coming in a day before and asking for having trouble with the invitations. So, please make sure that you uh at least accept the invite even if you don't start the actual assignment. All right. Okay. So, in the last lecture, uh I
0:48 mentioned that we were going to start object relationships today. um we will be doing that but I'm going to delay that just a little bit for a while until we speak about abstract classes and methods. Uh this is an important topic and I'd also like to spend some time today uh looking at an example of applying inheritance and abstract classes and methods concepts to a slightly larger example and that should hopefully help you in assignment number two as well. and then we'll start uh as much as possible time remaining uh look at object relationships between different styles of uh of designing object-oriented programs. All right. So let's start by looking at
1:36 the concept of abstract classes and methods as opposed to concrete classes. So uh we'll start off by looking at our lecture 4 material. Uh if you recall we had a student class and this was on our journey going from um uh an example where we weren't using inheritance to one where we were using inheritance. So we started off by looking at uh a model
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, Muhammad Sayeed Ghani.