It's preferable to write programs that depend on interfaces rather than on superclasses, but what if you want a class to have certain variables? Sometimes you want a class to implement a certain variable. (For example, imagine you have a datasource interface, and want all classes to implement a String user).In this case, I can see making an abstract class datasource, which other classes could then extend. But this means they can't extend another class anymore, due to no multiple inheritance in Java.So, to conclude, if I would have the above scenario, should I rethink my design, or is there a pattern I don't know about that I could follow to achieve the interface-with-variable design?
Implements > extends, but what about variables?
java;design patterns;interfaces;inheritance;multiple inheritance
If you need to share implementation, then use inheritance.  That's what it's for.  But use an interface as well, and treat the fact that inheritance is being used by the implementations as an implementation detail: client code shouldn't need to know.For instance, see the library classes List, AbstractList, ArrayList and LinkedList.  There are common implementation details of the two concrete classes which are shared by them both inheriting from AbstractList.  But as a client of them, this is an unnecessary detail which we don't ever use... we only care that they both implement the interface, List.