Is the following ok? (keep in mind that I didn't write the bodies of the classes and I also didn't write the interfaces ;-))
abstract class SuperClass implements SuperInterface
class SubClass extends SuperClass implements SubInterface
Or is this generally considered bad practice?
What made me wonder is, that the following didn't work:
List<SubInterface> myList;
...
for(SuperInterface si : myList) {
...
}
It's neither good nor bad. SubClass
here implements both SuperInterface
and SubInterface
(as well as interface defined by SuperClass
' public methods). If that's what you need - that's fine.
As for your second example
List<SubInterface> myList;
...
for(SuperInterface si : myList) {
...
}
You declared the list of SubInterface
elements, but want to fetch SuperInterface
elements from it. If SubInterface
extends SuperInterface
then this has some sense. Otherwise not.
It is correct. Why not ?
Your SuperClass implements a SuperInterface which also implemented by your SubClass (thanks to the the SubClass extends SuperClass).
In addition, your SubClass implements another interface (SubInterface).
There is nothing wrong in your code/architecture.
SuperClass -- implements --> SuperInterface
SubClass -- extends --> SuperClass -- implements --> SuperInterface*, SubInterface
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With