简体   繁体   中英

How to override method from parent class

I'm working on an Android project and I'm trying to override a method from a parent class. I still want the method to work all the same, only thing is that I need to add one more line. This is the method in my BaseFragment:

protected void setUpToolBar(String title, boolean home){
    Toolbar toolbar = (Toolbar)getView().findViewById(R.id.toolbar);
    if (toolbar != null){

        toolbar.setTitle(title);

        if(home){
            toolbar.setNavigationIcon(R.drawable.arrow_left);
        }


        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                return false;
            }
        });

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
    }
}

I want to keep all the stuff working when I call from my FeedFragment (which extends from BaseFragment) and add this line:

toolbar.inflateMenu(R.menu.menu_main);

I need that because the menu on FeedFragment is different from the one on PostFragment and both fragments inherit from BaseFragment.

I think I should do something like:

@Override
private void setUpToolBar(){
    toolbar.inflateMenu();
}

It's not working though. Any ideas on this?

obs: this question was marked as a possible duplicate but the other one is quite theoretical and I don't think it would help me to figure out how to solve my problem. the chosen answer just hit the nail on the head.

you should override exactly the same method signature

@override
protected void setUpToolBar(String title, boolean home){
    super.setUpToolBar(title,home);
    toolbar.inflateMenu();
}

You are looking for the super keyword. You can call method foo from parent class using super.foo() in the overriding method.

Here an extract :

"Accessing Superclass Members

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass:

public class Superclass {

public void printMethod() {
    System.out.println("Printed in Superclass.");
}

}

Here is a subclass, called Subclass, that overrides printMethod():

public class Subclass extends Superclass {

// overrides printMethod in Superclass
public void printMethod() {
    super.printMethod();
    System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
    Subclass s = new Subclass();
    s.printMethod();    
}

}

Within Subclass, the simple name printMethod() refers to the one declared in Subclass, which overrides the one in Superclass. So, to refer to printMethod() inherited from Superclass, Subclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following:

Printed in Superclass. Printed in Subclass"

You need to make a call to the parents class method using super

@override
protected void setUpToolBar(String title, boolean home) {
    super.setUpToolBar(title,home);
    toolbar.inflateMenu();
}

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