1

I am implementing a library that will be used by many different types of media players. I need to use a getPosition method that will vary for each individual user's video player.

Currently to accomplish this I have made my library an abstract class the user has to extend and then override the getPosition method with whatever object/method they use to get the position of their media player.

I am trying to make it so my class does not have to be abstract and that they could define this getPosition method for my library without having to extend my abstract class (i.e.) I don't want my library class to be abstract.

I have looked into using Reflection, but I am struggling with it because I do not know the class name/method name/parameters until runtime and I want the position of a specific instance of the media player object so I can get the most current position.

What's good practice for the Java/Android programming paradigm?

Is reflection the way to go? And if so could I see some sample code for this specific instance (Unknown class name/method name/parameters to be used by my imported library etc...)

Or is there a better way to accomplish this/is making my class abstract the way to go in Java?

Again I would prefer if the user did not have to extend my class and could just call some sort of setGetPosition() method on an object.

EDIT: Simply put: I want to be able to use an unknown class's getPosition method in my library, but I don't want my entire class to be abstract

5
  • 1
    I'm not sure I understand how reflection is coming into this question. If you want to enforce a method signature without using an abstract base class, create an interface. You might want to tighten the question up to provide some further clarification. Commented Jun 5, 2012 at 16:52
  • I'm sorry, but it usually is good programming practice to leave abstract classes as abstract until you use them [directly]. Commented Jun 5, 2012 at 16:52
  • Don't use reflection if you can avoid it, is slow as hell on Android/Dalvik Commented Jun 5, 2012 at 16:52
  • So reflection = bad, for android. But @DaveNewton are you recommending that I create a position interface that contains a getPosition method that the user can define? Simply put: I want to be able to use an unknown class's getPosition method in my library, but I don't want my entire class to be abstract Reflection was just a possibility that I was struggling with to accomplish this potentially. Commented Jun 5, 2012 at 17:49
  • @VinC Moved to answer; bit much for a comment. Commented Jun 5, 2012 at 18:03

1 Answer 1

2

That's what interfaces are for--to enforce API obligations.

Classes implementing Positionable (or whatever) can be (a) referenced as a Positionable, and (b) handle calls to Positionable's methods, like getPosition.

Consider the List interface: any class that implements List must have a bunch of specific methods, but they don't need to be a subclass. They may be a subclass, but don't have to be.

In general, "plugin" systems allow arbitrary implementations of specific functionality, and the functionality is specified using an interface. Interfaces are one of the most basic mechanisms Java provides for forcing classes to adhere to a spec.

Plugins may use reflection, but there's additional overhead, additional complexity, and allows undesirable runtime behavior precisely because there's no compiler enforcement as with interfaces.

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

1 Comment

I used an interface to implement a positionable object and passed that into my constructor and it worked! Thank you very much for all your help Dave, I really appreciate it.

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.