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?

Recommended Answers

All 5 Replies

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.

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?

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.

Right. So what are you trying to achieve here? What's this in aid of?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.