nus-cs2103-AY2223S1 / forum

14 stars 1 forks source link

Overriding method in abstract class #495

Open jhchee18 opened 1 year ago

jhchee18 commented 1 year ago

The model answer for the practice paper part 2 shows that the abstract class of ProgressWatcher does not have an overriding update() method, while UiWidget has one. At the same time, the solution in part 3 also does not include the overriding method. Does anyone have any idea why?

image

malwaregarry commented 1 year ago

A similar diagram was demonstrated in the week 8 tutorial. image

If I'm not wrong, the reason for not leaving update() in the abstract class is to signal that the update() method originated from the interface.

cheeheng commented 1 year ago

StackOverflow Reference

Since ProgressWatcher does not overwrite update() from Watcher, we do not write update() in the UML diagram.

However, for UiWidget(), update() method overrides update() from the parent class ProgressWatcher (which in turn inherits the update() method from Watcher), hence we have to explicitly write update() to indicate that it is overwritten. I believe it would be better to explicitly include this inside the textbook to prevent confusion of future batches.

From the StackOverflow source, {redefines} could be added after the method name, but since this is not covered in the module, we can just leave it out?

I am not so sure whether this is correct. Please correct me if I am wrong.

damithc commented 1 year ago

@jhchee18 ProgressWatcher doesn't have the update method because as per the code given, the programmer has decided not to have it there. If your point of confusion is 'How come ProgressWatcher can skip implementing a methods specified in an interface it claims to implement?', it is because ProgressWatcher is an abstract class. An abstract class can omit methods that a normal class would be obliged to implement -- those unfulfilled obligations of the abstract class are passed down to child classes instead. That's why the UiWidget class has to implement the update method (unless that class is abstract as well).