0

I have several queues that all extend a Queue class. I want to do a Queue manger base class to handle adding to the queue (and other operations).

If you take a look at the following code, I created a base class called QueueManger that has a static method called add() that will get 'Queue' and add a variable to it. I want to make getQueue() (called in add() ) abstract because I want to force other classes to provide their own implementation of the 'Queue'. The issue is I can't declare abstract static method. What is the right way to solve this?

public class RegistrationQueueManger { @Override Queue getQueue() { return new RegistrationQueue(); // RegistrationQueue is a class that // extends Queue.java } } public abstract class QueueManger { abstract static Queue getQueue(); // Error: cant do! (in java) public static Add(String myvar) { getQueue().add(myvar); } } 

Note: I saw some question about this subject but it didn't really give a good answer of how to solve it and create another design to handling it.

3
  • Read answers below, and use singleton pattern instead of your code Commented Jan 12, 2012 at 17:03
  • 1
    If he wants to extend the QueueManager, it should not be static or a singleton. Commented Jan 12, 2012 at 17:34
  • Right, what about making RegistrationQueueManger a singleton? is that right. Commented Jan 12, 2012 at 22:00

3 Answers 3

3

Only instance methods may be overridden. So abstract on a static method doesn't make sense. The getQueue method shouldn't be static, and the add method shouldn't be static either.

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

Comments

1

That's because abstract method has to be overridden (implemented) and static method is not inherited. Anything that's static belongs to the class itself where it's declared. So, if you if you make getQueue() static it will be a class method that will belong to the class QueueManger class itself. So, make it an instance method, so that every class that extends the QueueManger will have their own getQueue().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.