Abstract variables in Java?
abstract, class, java, variables
Solution
I think your confusion is with C# properties vs. fields/variables. In C# you cannot define abstract fields, even in an abstract class. You can, however, define abstract properties as these are effectively methods (e.g. compiled to `get_TAG()` and `set_TAG(...)`).
As some have reminded, you should never have public fields/variables in your classes, even in C#. Several answers have hinted at what I would recommend, but have not made it clear. You should translate your idea into Java as a JavaBean property, using getTAG(). Then your sub-classes will have to implement this (I also have written a project with table classes that do this).
So you can have an abstract class defined like this...
public abstract class AbstractTable {
public abstract String getTag();
public abstract void init();
...
}
Then, in any concrete subclasses you would need to define a static final variable (constant) and return that from the `getTag()`, something like this:
public class SalesTable extends AbstractTable {
private static final String TABLE_NAME = "Sales";
public String getTag() {
return TABLE_NAME;
}
public void init() {
...
String tableName = getTag();
...
}
}
EDIT:
You cannot override inherited fields (in either C# or Java). Nor can you override static members, whether they are fields or methods. So this also is the best solution for that. I changed my init method example above to show how this would be used - again, think of the getXXX method as a property.
Problem
I am coming from c# where this was easy, and possible. I have this code: ``` public abstract class clsAbstractTable { public abstract String TAG; public abstract void init(); } ``` but Eclipse tells me I use illegal modifier. I have this class: ``` public class clsContactGroups extends clsAbstractTable { } ``` I want the variable and method defined in such way, that Eclipse to prompt me, I have unimplemented abstract variables and methods. How do I need to define my abstract class so I should be prompted to implement the abstracts? EDIT 1 I will create different classes for different db tables. Each class should have it's own TABLENAME variable, no exception. I have to make sure this variable is static each time when I create a new class that extends the abstract class. Then in the abstract class I will have a method eg: init(); If in this init() method I call TABLENAME, it should take the value from the sub-class. something like this should also work out ``` String tablename=(clsAbstract)objItem.TABLENAME; // where objItem can be any class that extended clsAbstract; ``` EDIT 2 I want a constant(static) defined in each class having it's name defined in abstract. - I define variable TABLENAME in abstract, but no value given. - I create a clsContactGroups, I should be prompted to implement TABLENAME, this is where gets some data. eg: TABLENAME="contactgroups"; - I create a second class clsContacts, I should be prompted to implement TABLENAME, this is where gets some data. eg: TABLENAME="contacts"; etc...