-2

Just hoping I could get someone to tell me I am doing this right or not, as I have never written something like this before. So I have to write a class named ProcessQueue that is a subclass of Vector, as well as a constructor that will define an empty queue. Also the Vector will be holding items of type "Object." So here it is...

public class ProcessQueue<Vector>{

ProcessQueue(){}


}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Sherifftwinkie
  • 391
  • 1
  • 13
  • 21

2 Answers2

7

Not, that's not right. That's generic, not extending.

Besides, it's not a good idea. Your ProcessQueue might have a collection of some sort underneath, but it need not extend. Prefer composition to inheritance.

If you must implement something, start by having an API that actually looks like a Queue. There's a certain behavior that's expected; make sure that yours conforms to it. The best way is to implement an interface that enforces it.

public class ProcessQueue<T> implements Queue<T> {
   // Have a private collection of some sort that provides the behavior that the interface requires
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
6

You're not extending Vector anywhere. Change your code to:

public class ProcessQueue<T> extends Vector<T> {

}

By the way, I don't recommend you using Vector at all. Check Why is Java Vector class considered obsolete or deprecated?. IMO if you have to do this it would be better extending ArrayList.

public class ProcessQueue<T> extends ArrayList<T> {

}

And in your code use a List:

List<SomeClass> lstQueue = new ProcessQueue<SomeClass>();

U̶n̶r̶e̶l̶a̶t̶e̶d̶ ̶t̶o̶ ̶t̶h̶e̶ ̶o̶r̶i̶g̶i̶n̶a̶l̶ ̶q̶u̶e̶s̶t̶i̶o̶n̶ , I think similar as duffymo, you should not extend any of the Java Collection classes until it is a must-do. Note that there are plenty of classes to hold almost any kind of common collections, and you have Queue and PriorityQueue that can help you in your real work.

The best solution would be as pointed by duffymo, composition instead of extending a Java Collection:

public class ProcessQueue<T> {

    private List<T> data = new ArrayList<T>();

    //rest of code...
}

Or creating a new implementation of an interface, like Queue or List.

public class ProcessQueue<T> implements Queue<T> {

    //Queue interface methods implementation...
}
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • T should be replaced by Object as well, correct? Thank you by the way! – Sherifftwinkie Mar 20 '13 at 20:49
  • Or possibly even `public class ProcessQueue extends Vector`, based on the question. – Duncan Jones Mar 20 '13 at 20:49
  • @Sherifftwinkie Nope, using `` means that it can hold anything. I've tested before posting the answer. – Luiggi Mendoza Mar 20 '13 at 20:50
  • @duffymo have you read the part below the line when I say *I think similar as duffymo, you should not extend any of the Java Collection classes until it is a must-do*? – Luiggi Mendoza Mar 20 '13 at 21:58
  • Yup, I did, but that's not what your answer says. (I'm the downvoter, and here's my reason why.) The code extends ArrayList. I think a better solution, if you must go this way, is to implement List and use composition of an ArrayList underneath. – duffymo Mar 20 '13 at 21:59
  • @duffymo answer re-edited, and yes, I agree with you in the best solution. – Luiggi Mendoza Mar 20 '13 at 22:04
  • If I implement Queue, then I have to either declare it abstract or Override all methods, that is not what I want. How do I work around that? Because I still need to instantiate objects for the class. – Sherifftwinkie Mar 23 '13 at 20:24
  • @Sherifftwinkie note that you can use a `Queue queue = new LinkedList();`as inner attribute if you don't want to implement `Queue` interface. As said by duffymo and me, you can opt for extending one of the Java Collection framework classes but that's not a great idea, I just extended the answer to show how it would be a better design. In the end, the design decision will be up to you. – Luiggi Mendoza Mar 24 '13 at 00:02