Abstract Classes and Named Constants
Hello, everyone!
I have a quick question relating to... well, if you read the title you already know.
If I had access to an IDE right now, I would check this quick, but I do not. Anyways, I best explain myself through example, so:
Code Java:
public abstract class AbstractSuperclass
{
//have abstract named constants
private static abstract final int CONSTANT_A;
private static abstract final int CONSTANT_B;
}
Code Java:
public class NonAbstractSubclass extends AbstractSuperclass
{
//constants given values
CONSTANT_A = 5;
CONSTANT_B = 2;
}
Is the above code legal? To put it in words, can I define abstract named constants in an abstract class, and then give those constants values in a non-abstract subclass of that abstract class?
Re: Abstract Classes and Named Constants
Member variables can't be abstract. Also you can't assign them later is they are marked "final". Or if they are "private", since in that case they won't be visible. Finally the assignment of values must be within some sort of block (including constructor or method).
So you end up with something like:
Code :
public abstract class AbstractSuperclass
{
static protected int CONSTANT_A;
static protected int CONSTANT_B;
}
class NonAbstractSubclass extends AbstractSuperclass
{
static
{
CONSTANT_A = 5;
CONSTANT_B = 2;
}
}
Re: Abstract Classes and Named Constants
Quote:
Member variables can't be abstract.
Okay. I understand that the example was poor; it was meant to demonstrate a simple idea, nothing more.
It appears I will go back to the drawing board.
Re: Abstract Classes and Named Constants
Yes - I wondered whether, by removing the "final", I was gutting your example of what you had hoped would be its usefulness. Perhaps it would help if you said how you want the classes to behave or what they ought to do.