0

I have a Record class. In this Record class i have a save() method which saves data to a database. I have several other classes that extend this Record class, for example, a Person class. In my Person class i would like to define the database table name and columns for the People database table. I would like to make these final static properties that all instances of Person reference. How do I let Record, the extended class, know this property will exist in all extended class? How do i define a static property for a class that the base class knows exists?

Would this be an appropriate use of an interface?

public class Record {
    protected String table_name;
    protected String[] table_columns;

    private void save(){
        //save data based on table_name and table_columns;
    }
}

public class Person extends Record{
    protected final static String table_name = "People";
    protected final static String[] table_columns = {"_id","name"}
}
2
  • Why do you want the base class to have this property, ie what is the goal ? Commented Apr 17, 2013 at 1:27
  • i want the base class to be able to save the record to the correct database table and be aware of what columns it needs to populate Commented Apr 17, 2013 at 1:30

2 Answers 2

2

Why the need for static final?

If you make Record abstract and change the table name and table columns to be abstract methods, you will be required to implement them in subclasses (and Record will be ensured of their presence).

I'm not sure why instances of Person would need to access this information, but it would still be possible, if necessary.

public abstract class Record {
  protected abstract String getTableName();
  protected abstract String[] getTableColumns();

  private void save(){
    //save data based on getTableName() and getTableColumns();
  }
}

public class Person extends Record {
  protected String getTableName() {
    return "People";
  }
  protected String[] getTableColumns() {
    return new String[] { "_id", "name" };
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

ooooohhhhhh. Never understood the use of abstract classes and interfaces until now. Thanks!
2

I think a good way to achieve your reqs is doing something like this: (I assume Record should be abstract)

public abstract class Record {
    protected abstract String getTableName();
    protected abstract String[] getTableColumns();

    private void save(){
        String table_name = getTableName();
        String[] table_columns = getTableColumns();
        //save data based on table_name and table_columns;
    }
}

public class Person extends Record{
    protected final static String table_name = "People";
    protected final static String[] table_columns = {"_id","name"}

    protected String getTableName(){
         return table_name;
    }

    protected String[] getTableColumns(){
        return table_columns;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.