Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP : Does each child class get its own static variable if defined in Parent?

Tags:

java

oop

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)
like image 982
stackoverflow Avatar asked Dec 05 '25 19:12

stackoverflow


2 Answers

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 to this).

abstract class A
{
  protected abstract Queue<String> getQueue();
  public abstract void myAbstractMethod();

  public doConcreteThings()
  {
     //busy code utilizing a 'q'
     getQueue().add("something");
     myAbstractMethod();
     //busy code
  }
}

class B extends A
{
  private static Queue<String> q = new ArrayList<String>();
  protected Queue<String> getQueue() { return q; }

  public void myAbstractMethod()
  {
     //creates concrete implementation using 'q'
  }
}

class C extends A
{
  private static Queue<String> q = new ArrayList<String>();
  protected Queue<String> getQueue() { return q; }

  public void myAbstractMethod()
  {
     //creates concrete implementation using 'q'
  }
}
like image 126
John Kugelman Avatar answered Dec 08 '25 09:12

John Kugelman


Will each extended class get its own static Queue?

  • No, static variables are bound to a particular class in which you define it. If you want different static variables for each of your child class, you would need to define them explicitly.

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

  • That's not possible, as for the reason stated in the first case. Different classes don't share static variables. Rather different instances of same class share them.

However, you can access the static variables defined in Parent class in your Child class with the Parent Class name.

WorkAround: -

You can define getters & setters (non-static) for your queue in each of your subclasses, and also have different static queue for each of them. Now, everytime the method of a subclass is invoked (through polymorphism), it will return the static queue defined in that class only.

like image 22
Rohit Jain Avatar answered Dec 08 '25 10:12

Rohit Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!