简体   繁体   中英

Override parent's inner interface?

I have two classes which are very similar except for a few methods. For this reason I want to have all methods in one class (Parent) and then extend Parent from Child overriding a few of it's methods and interfaces. For what I've seen declaring a method in the child directly overrides the same method from the parent, however I haven't been able to override the parent interfaces. This is what I'm trying:

Parent class:

public class ContactList extends ListFragment implements SwipeListener, AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {

    //...

    public interface ContactsQuery {
        final static int QUERY_ID = 1;
        final static Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;

        @SuppressLint("InlinedApi")
        final static String SELECTION = Contacts.HAS_PHONE_NUMBER + "=1" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1" + " AND " + (Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) + "<>''";

        @SuppressLint("InlinedApi")
        final static String SORT_ORDER = Utils.hasHoneycomb() ? Contacts.SORT_KEY_PRIMARY : Contacts.DISPLAY_NAME;

        @SuppressLint("InlinedApi")
        final static String[] PROJECTION = {
                Contacts._ID,
                Contacts.LOOKUP_KEY, Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME,
                SORT_ORDER,
        };

        // The query column numbers which map to each value in the projection
        final static int ID = 0;
        final static int LOOKUP_KEY = 1;
        final static int DISPLAY_NAME = 2;
        final static int SORT_KEY = 3;
    }

Child class:

public class ContactListFavs extends ContactList {
    public interface ContactsQuery {
        final static int QUERY_ID = 1;
        final static Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;

        @SuppressLint("InlinedApi")
        final static String SELECTION = Contacts.STARRED + " AND " + Contacts.HAS_PHONE_NUMBER + "=1" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1" + " AND " + (Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) + "<>''";

        @SuppressLint("InlinedApi")
        final static String SORT_ORDER = Utils.hasHoneycomb() ? Contacts.SORT_KEY_PRIMARY : Contacts.DISPLAY_NAME;

        @SuppressLint("InlinedApi")
        final static String[] PROJECTION = {
                Contacts._ID,
                Contacts.LOOKUP_KEY, Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME,
                SORT_ORDER,
        };

        // The query column numbers which map to each value in the projection
        final static int ID = 0;
        final static int LOOKUP_KEY = 1;
        final static int DISPLAY_NAME = 2;
        final static int SORT_KEY = 3;
    }
}

However I cannot figure out how to do it. When instantiating a Child class it uses the interface from it's parent. How can I override it with the child's method?

You can Do it Like this...

class Parent
    {
        interface myP{
            public int good();
            public int good2();
            public int good3();
        }

    }

class Child extends Parent
    {
      interface myC extends Parent.myP{
            public int good();
            public int NOTgood();
        }
    }

What will happen here is if you extend Child and implement Child.myC you have to implement all methods which are in this case..

Child Interface (myC) Methods:

public int good();
public int NOTgood();

Parent Interface (myP) Method:

public int good();
public int good2();
public int good3();

But you need to keep access modifier interfaces public ,Default or protected but not private

Here in both interfaces there is a common method good() .

Instead of class Parent extending try to use and implement interface for both classes (super and subclass). The version is a bit more verbose but although it's more maintainable and clear for code reuse and modification:

public interface iCalculate 
{
    public int calculate();
}

public class Parent implements iCalculate
{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        int res = calculate();
    }

    public int calculate()
    {
        return 1 + 1;
    }
}

public class Child implements iCalculate
{
    public int calculate()
    {
        return 2 + 2;
    }
}

If you are on Java 8 you could use default methods in interface implementation to achieve traditional inheritance functionality. This time (in interface default methods case) you don't need to implement a method when default behavior is appropriate for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM