Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do we represent inheritance from a generic class? #41

Open
xenthm opened this issue Oct 31, 2024 · 2 comments
Open

How do we represent inheritance from a generic class? #41

xenthm opened this issue Oct 31, 2024 · 2 comments

Comments

@xenthm
Copy link

xenthm commented Oct 31, 2024

Question

In my team's project, the classes AuthorList and MangaList both inherit from the generic ArrayList. The exact code used is of the format:

public class AuthorList extends ArrayList<Author> {
    ...
}

Is the method of representing this inheritance structure depicted in the referenced picture valid? If not, how else can we represent it?

Reference

Screenshot 2024-10-30 132426
In this diagram, MangaList and AuthorList are indicated as inheriting from ArrayList, but with notes stating "ArrayList" and "ArrayList". I think that, unless these two classes inherit from ArrayList (in which I think you should remove the notes), I believe that there is no inheritance to indicate?

Originally posted in a comment in nus-cs2113-AY2425S1/tp#9 (comment)

@okkhoy
Copy link

okkhoy commented Nov 2, 2024

Your notation seems OK (* good!). Not sure what the comment means specifically.

From UML specification:
image

*we don't cover this in CS2113; so you went beyond to learn the notation for generics inheritance.
specifying the type in the UML note seems OK to me as a way to explain to the reader what it means.

However, I do have a comment to make:

Instead of extending from ArrayList, why not make it into a composition/aggregation where ArrayList is inside of a class that manages Author list?

@xenthm
Copy link
Author

xenthm commented Nov 2, 2024

Thanks for the reassurance Prof!

Instead of extending from ArrayList, why not make it into a composition/aggregation where ArrayList is inside of a class that manages Author list?

As for this, we initially did exactly that. We had an ArrayList<T> inside our AuthorList class. But we found that it was troublesome to keep making trivial methods to add and remove items from the private ArrayList (I think this happened when we wanted to create our second list class). So, we just made it inherit from ArrayList so we could use its add and remove methods as-is. More complicated methods were then added to extend the functionality of the ArrayList.

But then again, I guess having those trivial wrapper methods can help with method readability when using them outside. We were lazy at the moment 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants