Interfaces and code duplication

Started by
9 comments, last by WoopsASword 7 years, 8 months ago

To start, my background is C++, Just tinkering really; a few simple 3D games but nothing professional. Anyway.

I'm starting developing for my Android phone, which realistically means learning Java. Its been pretty easy so far as the two languages are similar.

However, I'm getting a little lost on the use of Interfaces. Every Java resource I've read goes crazy on the idea of interfaces, and uses them for almost everything. What I don't get however, is how they are better than plain old inheritance. The only thing they seem to be able to do is allow (a form of) multiple inheritance, which you're not allowed to do in Java by just extending classes, and in the cases I've been sketching out in readiness for coding, it appears to mean a lot of redundant code.

Say I have a class GameObject, being the abstract base class for everything in my game. Something in the game might be a visible physics object, so if I want an object that does that using Interfaces, I would write a Renderable interface and a Physics interface and implement both.

But say I have an object which doesn't use physics. I would have it implement just the Renderable interface.

Now I have my two objects which I can store in a collection and pass to a Renderer, the interfaces guaranteeing that both objects have the appropriate methods to be able to be drawn.

However, because Interfaces only contain the method declarations, not any actual functionality itself, in this case I'd have to write the drawing methods TWICE, for both the renderable physics object and the plain non-physics object.

In C++, I'd just have them inherit from a Renderable and a Physics base class to give them all the functionality they need, but in Java I can't do that as you can only inherit from one class.

Given this restriction, currently, I'm looking at just having a Renderable class extend the Base class, and the Physics object extend the Renderable without using Interfaces at all. But given how much they're pushed in the various coding resources, I can't help but think I'm missing something.

Why are Interfaces used instead of just Inheritance alone?

The first reason we want interfaces to exist is because we want to write a class based on behavior and not state.

If your interface includes only get/set methods, meaning something is wrong with your definition of your object. If you want containers, just use an existing class (List,Tuple,W/e...)

The second reason is abstraction. By defining an interface you force the implementation to work by abstraction rather than implementation.

This rules out any unwated behavior or implementation details.

The last reason I'll point out is seperation of concerns.

A really famous pattern is the strategy pattern. It allows us to seperate logic and make it replacable in case we actually want to make another implementation.

This allows us to generalize systems and seperate them into sub-systems, thus making it easier to code because you work on a sub-system rather than the whole system.

A fun fact, it allows humans to be more productive since comprehanding huge systems is a difficult task while trying to understand few classes is much easier.

There are more reasons you want interfaces, I advise you to research for yourself.

I use interfaces for C++ as well, making plain objects is nice, not when it come to designing big infrastructures or systems.

This topic is closed to new replies.

Advertisement