nus-cs2113-AY2425S1 / forum

3 stars 0 forks source link

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

Open xenthm opened 1 week ago

xenthm commented 1 week ago

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 https://github.com/nus-cs2113-AY2425S1/tp/pull/9#discussion_r1821904048_

okkhoy commented 5 days ago

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 commented 5 days ago

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 😃