Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract variables in Java?

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...
like image 346
Pentium10 Avatar asked Mar 03 '10 12:03

Pentium10


People also ask

Can we use abstract for variable?

No you cannot. When you declare a variable you declare an instance of the class/struct you specified. Abstract classes cannot be directly instantiated.

What is abstract in Java with example?

Abstraction in Java Abstraction is a process of hiding the implementation details and showing only functionality to the user. Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message.

What is the type of variable in abstract class?

Type of variables: Abstract class can have final, non-final, static and non-static variables. The interface has only static and final variables. Implementation: Abstract class can provide the implementation of the interface. Interface can't provide the implementation of an abstract class.

Can abstract class have variables Java?

Abstract classes can have instance variables (these are inherited by child classes). Interfaces can't. Finally, a concrete class can only extend one class (abstract or otherwise).


1 Answers

Define a constructor in the abstract class which sets the field so that the concrete implementations are per the specification required to call/override the constructor.

E.g.

public abstract class AbstractTable {     protected String name;      public AbstractTable(String name) {         this.name = name;     } } 

When you extend AbstractTable, the class won't compile until you add a constructor which calls super("somename").

public class ConcreteTable extends AbstractTable {     private static final String NAME = "concreteTable";      public ConcreteTable() {         super(NAME);     } } 

This way the implementors are required to set name. This way you can also do (null)checks in the constructor of the abstract class to make it more robust. E.g:

public AbstractTable(String name) {     if (name == null) throw new NullPointerException("Name may not be null");     this.name = name; } 
like image 98
BalusC Avatar answered Oct 08 '22 17:10

BalusC