0

Google provides such an example here: http://developer.android.com/training/basics/data-storage/databases.html#DefineContract

public final class FeedReaderContract {

    // To prevent someone from accidentally instantiating the contract class,
    // give it an empty constructor.
    public FeedReaderContract() {}

    /* Inner class that defines the table contents */
    public static abstract class FeedEntry implements BaseColumns {
        public static final String TABLE_NAME = "entry";
        public static final String COLUMN_NAME_ENTRY_ID = "entryid";
        public static final String COLUMN_NAME_TITLE = "title";
        public static final String COLUMN_NAME_SUBTITLE = "subtitle";
        ...
    }
}

Could someone please explain me in detail why is that FeedReaderContract class made final and FeedEntry made abstract? I've seen such approach in many sources and it's clear why FeedEntry's fields are static, but I have no idea why FeedEntry itself is abstract and FeedReaderContract is final. Can we define these classes without these modifiers? What will be the difference? I can't find relevant info anywhere. :(

Eugene Garbuzov
  • 193
  • 1
  • 4
  • 9
  • 1
    Intention is to prevent Instantiation (abstract) and to prevent Inheritance (final) of the Contract. – Fildor Nov 02 '15 at 10:37
  • Yes, but why do we need to prevent instantiation and inheritance? Why are they unwanted? – Eugene Garbuzov Nov 02 '15 at 10:47
  • 1
    Because it's only a contract. You don't want to have something like `FeedReaderContract fcr = new UserDefinedContractChild()`. A User can extend FeedEntry, though making sure it has the columns defined by the contract. Look at it like an Interface. – Fildor Nov 02 '15 at 11:02
  • And... Why don't we want something like FeedReaderContract fcr = new UserDefinedContractChild() ? – Eugene Garbuzov Nov 02 '15 at 18:24
  • 1
    Because we want to be free from that dependency. We just want to publish an interface to all who want to code FeedReaders. And the Contract says: FeedReaders' FeedEntries need to have ColumnXYZ ... – Fildor Nov 03 '15 at 09:52

0 Answers0