nus-cs2103-AY2021S1 / forum

20 stars 2 forks source link

Can we assume that interface has no default implementation? #524

Closed dhafinrazaq closed 3 years ago

dhafinrazaq commented 3 years ago

Hi Prof @damithc ,

In the question below, it seems that interface is assumed to be pure (without default implementation). This is because if the interface Goo has a default implementation for store(), I think option A should also be incorrect (Moo can be compiled). Example here

Screen Shot 2020-12-01 at 4 30 28 PM

However, in this quiz, we do not have such assumption.

Screen Shot 2020-12-01 at 4 25 18 PM

In the exam, can we assume that interface/abstract class has no default implementation (especially if the method name is listed in the UML diagram)?

Thank you

GeNiaaz commented 3 years ago

An interface has its methods declared abstract and public by default. By virtue of that, the subsequent class (Moo) HAS to implement an interface to instantiate the abstract method or else be an abstract class itself. Hence why option A is correct, which also does not conflict with the quiz question either.

dhafinrazaq commented 3 years ago

Thanks and I'm not sure if I understand it correctly, but if it is the case where the Goo#store() has a default implementation (for example, inside the interface Goo, default void store() { System.out.println(""); } is declared), Moo does not have to override/declare its own store() method, and thus Moo can be compiled (I've already tried to compile a class that implements an interface with default method implementation, example here).

Wincenttjoi commented 3 years ago

All classes that implement an interface must implement ALL the methods or else it will not compile, in this case Moo class needs Moo#store() for it to compile. But when it becomes an abstract class, it can compile without Moo#store()

shadowezz commented 3 years ago

Is it possible to represent default methods in interface in a uml diagram?

joshtyf commented 3 years ago

By default you are referring to the visibility modifier right? If that is the case, the default is package private. So you can technically show the visibility using the tilda symbol "~"

GeNiaaz commented 3 years ago

@joshtyf i think he's referring to methods in an interface, so the default should be public, or "+"

dhafinrazaq commented 3 years ago

@joshtyf I think the default refers to method declared with a default implementation

Screen Shot 2020-12-01 at 6 56 33 PM

joshtyf commented 3 years ago

Ah okay my bad. Hmm, then I think default methods should just be represented in the same way as you would for a normal method.

damithc commented 3 years ago

In the question below, it seems that interface is assumed to be pure (without default implementation). This is because if the interface Goo has a default implementation for store(), I think option A should also be incorrect (Moo can be compiled). Example here

Screen Shot 2020-12-01 at 4 30 28 PM

That option is considered the correct answer because if the statement doesn't indeed compile, it has nothing to do with Foo being an abstract class.

Assume there are no default methods if not specifically mentioned.

dhafinrazaq commented 3 years ago

I see, thanks prof and everyone else