1

The question has mostly to do with how inheritance and extension works.

I currently have the following:

public class Client extends BaseClient { ... } and

public class BaseClient extends MinimalisticClient { ... }

The main difference between them is the amount of parameters (data) they hold (fetched from a database).

What I want to do is create an interface for each individual one that would contain only the getters.

so, it would be something like:

public interface IMinimalisticClient { getters }
public class MinimalisticClient implements IMinimalisticClient { ... }

public interface IBaseClient extends IMinimalisticClient { the extra getters }
public class BaseClient extends MinimalisticClient implements IBaseClient { ... }

public interface IClient extends IBaseClient { the extra extra getters }
public class Client extends BaseClient implements IClient { ... }

Taking BaseClient as an example. When I extent MinimalisticClient, I implement the IMinimalisticClient into BaseClient by default. But then, I also implement IBaseClient which extends IMinimalisticClient as well. Is this acceptable? Am I doing something completely stupid?

Just as a mention, I am doing the way I presented it only because the software will run on cheap android devices and I have to manage the memory usage as much as possible.

Alex Jeefo
  • 55
  • 5
  • 3
    Yes; that's fine. – SLaks Feb 06 '18 at 14:15
  • https://stackoverflow.com/questions/19546357/can-an-interface-extend-multiple-interfaces-in-java – Suresh Atta Feb 06 '18 at 14:19
  • A side note: if I had to choose between a prefix for a interface or implementation, I'd do it for the implementation class. – DPM Feb 06 '18 at 14:24
  • If the interface hierarchy makes sense in general, there's no problem in some class inheriting same interface several times. This will only make a difference if you query them via reflection. – M. Prokhorov Feb 06 '18 at 14:47

2 Answers2

1

This works fine, but you should ask yourself why you need the interfaces. If you do not have multiple implementations of the interface, there doesn't seem to be a point why you would have them in the first place.

kutschkem
  • 7,826
  • 3
  • 21
  • 56
  • I am doing it mostly for making sure there is no way some parts of the code can have access to the setters. With great power comes great responsibility. So I have to protect myself from my own stupidity in case I have a stroke and call a setter somewhere. – Alex Jeefo Feb 06 '18 at 14:33
  • @AlexJeefo that's what private and protected are for. – kutschkem Feb 07 '18 at 07:31
  • True, but in my case I don't want them to be completely inaccessible as I actually need to use the public setters in some particular place. Think of it that way: you create a new Client, add it to the database. When I display it, I don't want access to any setters. But if you want to edit the client, you need access to the setters (or create a new Client object). As I am programming for Android, I want only the Activity dealing with editing to use the concrete objects and have access to the setters. Any other activity shall store the clients by its interface. – Alex Jeefo Feb 07 '18 at 12:13
  • @AlexJeefo If you have a reason it is good. What is bad is if you complicate your code for no reason. It seems you made a conscious decision here, which is all I wanted to point out you should do. – kutschkem Feb 07 '18 at 13:35
0

Yes, it is acceptable. Since interface is not a parent class ( and can not be walked up the hierarchy ) it is perfectly acceptable to have the same interface multipe times in implementations

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
  • Thanks for the answer. I just started implementing it. It seems like it's forcing me to override the methods again (BaseClient is forced to override the IMinimalisticClient methods, something I do not really want as I wanted to keep on using the ones already defined in the class I am extending). Is there any way to go around this? – Alex Jeefo Feb 06 '18 at 14:36
  • Never mind my previous comment. I just screwed something up myself. All is fine. Thank you – Alex Jeefo Feb 06 '18 at 14:41
  • But of course, inheritance between interfaces is direct acyclig graph - so there is no way to have multiple inheritance between interfaces – Konstantin Pribluda Feb 06 '18 at 16:32