0

So this is more of a question of curiosity rather than needing an issue solved, I think.

So I am sitting with AndEngine for Android in Android Studio and I want to make a AudioManager to handle all my audio, as a start.

I could implement AndEngine's interface called IAudioManager or extend BaseAudioManager, which is already an implementation of previously mentioned interface. Which brings me to my question. IAudioManager and IAudioEntity:

public interface IAudioManager<T extends IAudioEntity> {

    public float getMasterVolume();
    public void setMasterVolume(final float pMasterVolume);

    public void add(final T pAudioEntity);
    public boolean remove(final T pAudioEntity);

    public void releaseAll();
}

public interface IAudioEntity {
    public void play();
    public void pause();
    public void resume();
    public void stop();

    public float getVolume();
    public void setVolume(final float pVolume);

    public float getLeftVolume();
    public float getRightVolume();
    public void setVolume(final float pLeftVolume, final float pRightVolume);

    public void onMasterVolumeChanged(final float pMasterVolume);

    public void setLooping(final boolean pLooping);

    public void release();
}

Now if I want to make an AudioManager using this interface it can be typed as T extends IAudioEntity. But I thought that you implemented interfaces instead of extending them, right?

So I am just wondering if there's something I have missed when it comes to interfaces. I'm not that keen on Java but I had a course at uni so I remember a lot of stuff but the details might be fuzzy, but does this just mean that the actual syntax in this case doesn't really matter as it would have to be implemented in order to be instantiated and anything extending that class in turn will be able to taken as argument in this AudioManager?

2 Answers 2

1

Its just syntax. When defining a class you indeed use implements to indicate that the class implements an interface and extends to indicate that the class is a subclass of another class.

When using extends within the < and > you are specifying the bounds for the type variable. Here A<T extends B> means that T must be a subtype (not subclass) of B. So the extends keyword can have a different meaning depending on the context in which it occurs.

The reason for this is that it is not easy to add keywords to the language without breaking code. If you add a new keyword (e.g. subtype) to the language, then code in which subtype is used as a variable name fails to compile. So when generics were added in Java 1.5, they chose to reuse the keywords extends and super rather than add new keywords.

Sign up to request clarification or add additional context in comments.

1 Comment

That's what I figured, but wanted to make sure. The suptype vs subclass difference I guess explains it. There more you know. :)
0

Now if I want to make an AudioManager using this interface it can be typed as T > extends IAudioEntity.

Nope, you can extend classes or implement interfaces. IAudioEntity is am interface, so you need to implement it.

Or you can extend BaseAudioManager, which is a class, implementing IAudioEntity.

Or you can do both, but that's mostly pointless.

Note that since java 8 (one day, it might even come to Android ;) ), the difference is thinner than ever since interfaces can bring code through the use of defender methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.