Example
abstract class A
{
protected static Queue<String> q = new ArrayList<String>();
abstract void myAbstractMethod();
public doConcreteThings()
{
//busy code utilizing a 'q'
q.add("something");
myAbstractMethod();
//busy code
}
}
class B extends A
{
public void myAbstractMethdo()
{
//creates concrete implementation using 'q'
}
}
class C extends A
{
public void myAbstractMethdo()
{
//creates concrete implementation using 'q'
}
}
- Will each extended class get its own static Queue?
- If not, how do I make sure that common functionality of a static variable is defined in the parent but each class gets its own static variable (hence a static Queue)
No, there will be one queue shared by all the classes. One way to do this would be to have a separate static queue in each sub-class and add another protected
getQueue()method that returns this queue. That way each sub-class can have its own queue.Note that
getQueue()would be a non-static method but return a static variable reference. That lets you implement it in sub-classes, while its behavior is “effectively” like a static method (it does not require access tothis).