Why do we have to call super in Android sometimes?

后端 未结 6 1087
暗喜
暗喜 2020-11-29 21:05

Sometimes when I override methods, I get an exception the first time it\'s called like below:

05-31 21:32:04.266: E/AndroidRuntime(28471): android.support.v4         


        
6条回答
  •  一个人的身影
    2020-11-29 21:53

    The super keyword has two main uses

    1. Calls the superclass’ constructor.

    2. Access a member of the superclass that has been hidden by a member of a subclass.

    So, why need to user super keyword sometimes ? Answer would be android comes under 4GL language which means it has many functionality ready made. While we are overridding these methods for the customization we use super keyword.

    see the very simple usage of super keyword in android ( as we do it most of the time ).

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        .
        .
        .
    }
    

    super() must always be the first statement executed inside a subclass’ constructor. When a subclass calls super(), it is calling the constructor of its immediate superclass. The second form of super is most applicable to situations in which member names of a subclass hide members by the same name in the superclass.

提交回复
热议问题