Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

public onCreate(), or protected onCreate()?

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?

like image 570
Soroor Avatar asked Sep 17 '12 14:09

Soroor


2 Answers

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

like image 164
Rahul Shah Avatar answered Dec 11 '22 12:12

Rahul Shah


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)

like image 32
njzk2 Avatar answered Dec 11 '22 12:12

njzk2