0

I know that it's possible to "force" a class to have a certain function through interfaces, but is there any way to "force" a class to have and assign certain variables? Also, is there way to "force" a class to assign inherited variables?

4
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by apines
0

Forcing a class to implement abstract methods is not the same as forcing a variable - you can force the class to have the variable via inheritance, but as far as I know you can't force it to initialize it.

0

You could put the initialization in the constructor for an abstract class, and hope they call super().
What about declaring a static final in the parent class? Would that do it?

0

You could put the initialization in the constructor for an abstract class, and hope they call super().
What about declaring a static final in the parent class? Would that do it?

That indeed does the job, but it's final, so it can't be altered afterwards. Therefore it's not that useful if we're using it to save object-specific attributes that change along the execution.

0

bleedi - as far as I know there is no option to do this. Most of the times you can use different approaches.

Edited by apines: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.