Before we start: I'm aware of this question and in my case that answer will not work.
So let's say I have:
interface Iface
class SuperClass (this one is from external library and can't be changed)
class SubClass extends SuperClass
class OtherClass
Is there a way to make it so that Iface
can only be implemented by SuperClass
and its subclasses? So in this case:
class SuperClass implements Iface - would be good if I could edit that class
class SubClass implements Iface - good
class OtherClass implements Iface - bad
Side question: is that against OOP principles or is it a sign of bad code to do so?
Iface
, it could show that you try to use the wrong tool to solve your problem.Iface
only makes sense for subclasses ofSuperClass
, but I want to keep it an interface. If I make them classes, I can't use multiple inheritance. If instead ofIface
I'd haveIface1
,Iface2
andIface3
, that would mean making 7 different classes and extending them instead of extendingSuperClass
and adding whatever interfaces are needed.PrintWriter
to implementsIterable
. But does that had an impact on the visibility/accessibility to theIterable
interface ? Of course not, because everyone should be allowed to implement an interface as long as the contract is fullfilled (=an implementation for the interface's methods is provided).SubClass
and mark all the methods eitherabstract
orfinal
.