public onCreate(), or protected onCreate()?
Asked Answered
T

3

6

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?

Torruella answered 17/9, 2012 at 14:13 Comment(3)
you can use public access modifier in all activity....Stoner
Can you tell us what did that program do?Hypothermal
It is a sudoku program, and as said before, all classes which has onCreate method -except in main activity- have 'protected void onCreate(Bundle savedInstanceState)'. WHY?? i CAN'T believe there not be special reason!!Torruella
B
1

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)

Bascule answered 17/9, 2012 at 14:49 Comment(0)
I
0

It can as well be protected. There is nothing important or wise about that.

Inflorescence answered 17/9, 2012 at 14:16 Comment(0)
D
0

There is no reason to make the method public (but doing so doesn't affect anything). You shouldn't call it manually anyway.

By the way Intellij IDEA have Activity template with public onCreate. This may be the reason this method public in the book.

Dorso answered 1/5, 2016 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.