While reading the "Hello, Android" book, I noticed that:
each java file with
onCreate(Bundle savedInstanceState)
method, has protected access modifier EXCEPT in the main Activity of the program [that has:public void onCreate(Bundle savedInstanceState)
].
Why is the onCreate
method public
on the program's main Activity, but protected
everywhere else?
the onCeate() is protected so to avoid calling of it from the activity object.
MyActivity activity = new MyActivity();
activity.onCreate(args); // which doesn't make sense because activity is not yet created
Since this method is only called when the activity gets created, calling it yourself will most probably give you a nullpointerException because the activity is not yet created. S/O Post
You can change the visibility of a method. What you cannot do is : reduce the visibility (make a public method private), access a private method (making it public)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With