Search code examples
javaclassinterfacebuilt-in

Java built-in class to interface


Can you create an interface that inherits all functions of an java built-in class?

What I am trying to do:

I have a lot of classes that extends Fragment, in the classes there are some attributes (get, set) and some functions. After making objects of the different classes I put them in one general Fragment list.

But I want to introduce polymorphism: 1 interface class and all the other classes implement that so they can override the functions.

But the problem is that if I want to use polymorphism I need an interface, and the interface should be an Fragment so I still can put these fragments in to a list, so could I create an interface from the Fragment class or can the interface inherit from the fragments?

On the image you could see the current situation

current

Update

Here is some code to show what I want to achieve:

So I have my main function which is an xmlSAXParser, when I encounter a tag I create an object of the correct class:

case template:
        switch (typeObjecten.toType(atType)) {
        case NOVALUE:
            break;
        case chapter:
            chapter = new ChapterFragment();
            typeObject = "chapter";
            break;
        case execute:
            execute = new ExecuteFragment();
            typeObject = "execute";
            break;
        case first:
            first = new FirstFragment();
            typeObject = "first";
            break; 
                              ...

When I found the end tag I add the right object to the list:

case template:
        switch (typeObjecten.toType(typeObject)) {
        
        case NOVALUE:
            break;
        case chapter:
            fragments.add(ownFrag);
            break;
        case execute:
            fragments.add(execute);
            break;
        case first:
            fragments.add(first);
            break;
        case navigation:
            fragments.add(navigation);
            break;
        case story:
            fragments.add(story);
            break;
                          ....

It is also possible that in every class the background could change, now I have to check which class the object is to call the correct function:

case background:
        
        if (typeObject.equalsIgnoreCase("navigation")) {
            navigation.setBackgroundColor("#" + text);
        } else if (typeObject.equalsIgnoreCase("chapter")) {
            //chapter.setBackgroundColor("#" + text);
        } else if (typeObject.equalsIgnoreCase("first")) {
            ownFrag.setBackgroundColor("#" + text);
            first.setBackgroundColor("#" + text);
        } else if (typeObject.equalsIgnoreCase("execute")) {
            execute.setBackgroundColor("#" + text);
        } else if (typeObject.equalsIgnoreCase("story")) {
            story.setBackgroundColor("#" + text);
        } else if (typeObject.equalsIgnoreCase("intro")) {
            intro.setBackgroundColor("#" + text);
        } else if (typeObject.equalsIgnoreCase("mission")) {
            mission.setBackgroundColor("#" + text);
        } else if (typeObject.equalsIgnoreCase("question")) {
            question.setBackgroundColor("#" + text);
        } else if (typeObject.equalsIgnoreCase("info")) {
            info.setBackgroundColor("#" + text);
        }
        break;
                   ...

So it would be nice if I could make an 'global' Fragment and then that the program finds out itself which function from which class it has to call (polymorphism?)


Solution

  • Another option is to make Fragment abstract and have your classes extend it. Then you could also add in some shared functionality if possible.

    Based on you comment that Fragment is a built in class and not editable it seems there are a couple of options. You could reference Objects extending Fragment generically <? extends Fragment> or by creating a wrapper class over Fragment that holds and maintains an instance of it. This would give you more control.

    A simple example could be something like the following.

    class CannotModify{
        public void doSomething(){}
    }
    
    
    
    
    public interface AddThisIfYouWant{
        public void doSomething();
    }
    
    
    
    
    public class Wrap extends CannotModify, implements AddThisIfYouWant{
        private final CannotModify yourInstance;
    
        public Wrap(){
            this.yourInstance=new CannotModify();
        }
    
        @Override
        public void doSomething(){
            super.doSomething();
        }
    
    }
    

    You could also just extend Fragment if you only need to reference your Objects at the Fragment level. For example:

    public class AnotherFragment extends Fragment{...}
    
    ...
    public void somethingWithFragments(List<? extends Fragment> fragments){
        for(Fragment fragment:fragments){
            ...
        }
    }
    ...