Timeline for What is the difference between public, protected, package-private and private in Java?
Current License: CC BY-SA 4.0
64 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 5 at 12:14 | comment | added | Neph | @wolfenblut I'm currently looking for the same thing: A modifier to make private variables of an abstract class accessible to its subclasses, without exposing them to every other class in the project. Sadly this doesn't seem to be possible, at least not in older Java versions. The closest solution would be to use no modifier (package private) and work with multiple packages - which isn't "pretty". | |
| Sep 19 at 12:28 | comment | added | David Balažic | The private keyword has more to it than mentioned. See this answer | |
| Sep 13 at 19:33 | history | edited | Basil Bourque | CC BY-SA 4.0 | The grey checkmark character was unreadable in Dark Mode. Replaced with green check. |
| S May 23, 2024 at 20:16 | history | suggested | Bolpat | CC BY-SA 4.0 | Use ✔️ and ❌ in table |
| May 23, 2024 at 16:56 | review | Suggested edits | |||
| S May 23, 2024 at 20:16 | |||||
| Nov 17, 2023 at 19:50 | comment | added | Jim Balter | @wolfenblut See stackoverflow.com/questions/69218585/… | |
| Nov 17, 2023 at 19:48 | comment | added | Jim Balter | "This answer is answering the question in the title, but not the question in the body." Which makes it a bad answer. (And many of the comments here are atrocious. "World is within your project"? Good grief.) | |
| Apr 26, 2022 at 10:03 | comment | added | wolfenblut | Is there any way to make it accessible from only a subclass, or any type of a friend class like in C++? | |
| Nov 23, 2021 at 8:35 | review | Suggested edits | |||
| Nov 24, 2021 at 1:19 | |||||
| Sep 26, 2021 at 20:19 | comment | added | Tin Lek | @niks The third column indicates whether subclasses of the class declared outside this package have access to the member according to Oracle documentation | |
| Jul 5, 2021 at 12:37 | comment | added | Marco Sulla | @DawoodibnKareem can you explain the real functioning of protected, or link a page that explain it better? | |
| Feb 17, 2021 at 7:38 | comment | added | M. Justin | @DawoodsaysreinstateMonica I think the issue here is that the title & body are asking two different questions: "What is the difference between the access modifiers" and "When should the different access modifiers be used." This answer is answering the question in the title, but not the question in the body. | |
| Feb 17, 2021 at 6:44 | history | edited | M. Justin | CC BY-SA 4.0 | Switch from ASCII table to Markdown table |
| Feb 5, 2021 at 2:57 | comment | added | Piovezan | @Jack Yeah, niks' comment is wrong despite the many upvotes stating otherwise. The Java Tutorials link in the answer clearly says that protected members can also be accessed within subclasses from a different package. It seems that he/she meant "package-level" instead of "protected", or was referring to a different edit. | |
| Nov 21, 2020 at 13:01 | review | Suggested edits | |||
| Nov 21, 2020 at 19:54 | |||||
| Oct 15, 2020 at 12:30 | comment | added | Jack | @niks protected members CAN be accessed by subclasses from a different package. Isn't that the point? Otherwise what's the difference between default and protected? | |
| Jul 29, 2020 at 22:46 | comment | added | jayjay | @niks : I am able to access protected members from subclass of different packages | |
| Jul 29, 2020 at 22:44 | comment | added | jayjay | @UsagiMiyamoto : Can you please elaborate more on how can "private members can be seen/used by any class/static method in the same source-file" ? | |
| May 8, 2020 at 10:18 | comment | added | Badri Paudel | protected is only accessible in same package. Its not accessible outside of that package . protected specifier in java is called package level specifier. | |
| Feb 6, 2020 at 14:58 | comment | added | Heinzlmaen | @DawoodsaysreinstateMonica It's a great answer, it's exactly what most people that find this page are looking for. Also, that table is from the official Oracle tutorial. But how about you toss us a link so we can learn what "protected" really means? | |
| Sep 5, 2019 at 6:50 | review | Suggested edits | |||
| Sep 5, 2019 at 7:03 | |||||
| S Jul 31, 2019 at 5:52 | history | suggested | Istiaque Hossain | CC BY-SA 4.0 | formate code |
| Jul 31, 2019 at 5:25 | review | Suggested edits | |||
| S Jul 31, 2019 at 5:52 | |||||
| S Feb 14, 2019 at 2:29 | history | suggested | Drew Nutter | CC BY-SA 4.0 | Restore to sane intelligible layout where you can actually read the column headers and you don't have wildly variable column widths. Also fixes the grid corner characters. |
| Feb 13, 2019 at 21:55 | review | Suggested edits | |||
| S Feb 14, 2019 at 2:29 | |||||
| Jan 30, 2019 at 22:03 | history | edited | StealthRT | CC BY-SA 4.0 | added 115 characters in body |
| Sep 6, 2018 at 7:04 | comment | added | Anatolii Stepaniuk | Subclass (same package) field is misleading, It's better to remove that column. Because it doesn't matter is that a subclass or not. | |
| Jul 11, 2018 at 6:26 | comment | added | Dawood ibn Kareem | Unfortunately, this answer is a gross oversimplification. Reality is a bit more complicated, especially when you consider protected (which is actually quite a difficult access modifier to fully understand - most people who think they know what protected means really don't). Also, as Bohemian pointed out, it doesn't answer the question - it says nothing about when to use each access modifier. In my opinion, this answer isn't quite bad enough to downvote, but close. But over 4000 upvotes? How did this happen? | |
| S Jun 25, 2018 at 19:47 | history | suggested | Mahozad | CC BY-SA 4.0 | made table with box-drawing characters |
| Jun 25, 2018 at 17:19 | review | Suggested edits | |||
| S Jun 25, 2018 at 19:47 | |||||
| May 4, 2018 at 12:25 | comment | added | adprocas | For example, if I have MyClass and I'm doing AnotherClass extends MyClass I will have access to all protected and public methods and properties from within AnotherClass. If I do MyClass myClass = new MyClass(); in AnotherClass somewhere - let's say the constructor - I will only have access to the public methods if it is in a different package. Note that if I do = new MyClass() { @Override protected void protectedMethod() { //some logic } }; it appears that I can access protected methods, but this kind of the same as extending it, but inline instead. | |
| May 4, 2018 at 12:22 | comment | added | adprocas | @Deepak, if you are extending (inheriting) the class then that class that is inheriting will have access to all public and protected methods and properties. If you instead instantiate that class in another class (not extend) and try to do something like object.protectedMethod() on a protected method, you won't be able to unless it is in the same package. | |
| May 4, 2018 at 6:23 | comment | added | Deepak | @adpro - I tried at my end making methods public and protected but both of these types of methods are accessible outside their primary package and user has to extend the parent class in both the cases which is quite obvious. I am still unsure of the difference. A little bit more explanation or trick might help. | |
| May 3, 2018 at 13:02 | comment | added | adprocas | World is within your project. I should explain further. Libraries are within your project, and if you're creating a library, they would expose these public classes and methods as well. So, saying just within your project is a bit off. "Everything that uses it" is a better description. | |
| May 3, 2018 at 12:59 | comment | added | adprocas | @Deepak, World is within your project. If you have a class that is public, any other class can instantiate that class. Same as methods. If it is public, anyone that uses that class can use that method. Package refers to the "folder" that you are in. So, ca.my.package.controllers is different than ca.my.package.models. Public items are accessible outside of those packages, but the rest are not. package-private are considered public within the package, essentially. Protected means anything that inherits it, or within the same package. Private removes all outside access, including package. | |
| Apr 25, 2018 at 12:25 | comment | added | Deepak | I am still not able to differentiate between 'Public' and 'Protected'. And what is 'World' in this answer. Does it mean access out of project folder in a different project & package?? But that is not taking place. Need help on this. | |
| Nov 17, 2017 at 22:16 | history | edited | ACV | CC BY-SA 3.0 | [Edit removed during grace period] |
| Jun 13, 2017 at 9:29 | history | edited | Pod | CC BY-SA 3.0 | Easier to read without the `o`s. |
| Dec 18, 2016 at 18:11 | review | Suggested edits | |||
| Dec 18, 2016 at 22:36 | |||||
| S Aug 25, 2016 at 18:27 | history | suggested | AndyW | CC BY-SA 3.0 | for quick glance (no need to read any text). |
| Aug 25, 2016 at 17:01 | review | Suggested edits | |||
| S Aug 25, 2016 at 18:27 | |||||
| Aug 22, 2016 at 11:08 | history | edited | David | CC BY-SA 3.0 | Adapted the table to reflect the first comment about package-private visibility to subclasses |
| May 19, 2016 at 10:07 | history | edited | Alex Salauyou | CC BY-SA 3.0 | A bit more illustrative and easy to remeber |
| Apr 22, 2016 at 6:07 | review | Suggested edits | |||
| Apr 22, 2016 at 6:34 | |||||
| Aug 28, 2015 at 16:09 | history | edited | user3453226 | CC BY-SA 3.0 | deleted 31 characters in body |
| Aug 25, 2015 at 19:10 | review | Suggested edits | |||
| Aug 25, 2015 at 20:12 | |||||
| Jul 20, 2015 at 12:26 | review | Suggested edits | |||
| Jul 20, 2015 at 13:14 | |||||
| Apr 25, 2015 at 14:47 | review | Suggested edits | |||
| Apr 25, 2015 at 15:26 | |||||
| S Jan 30, 2015 at 20:13 | history | edited | Steven Levine | CC BY-SA 3.0 | More description on what 'no modifier' means and an easy way to remember protected & default access |
| S Jan 30, 2015 at 20:13 | history | suggested | bighi | CC BY-SA 3.0 | More description on what 'no modifier' means and an easy way to remember protected & default access |
| Jan 30, 2015 at 19:39 | review | Suggested edits | |||
| S Jan 30, 2015 at 20:13 | |||||
| Jul 29, 2014 at 10:52 | history | edited | Raj | CC BY-SA 3.0 | updating the reference link |
| S May 25, 2014 at 12:06 | history | suggested | Samir | CC BY-SA 3.0 | improved the ASCII table layout by replacing the check mark and x mark |
| May 25, 2014 at 11:58 | review | Suggested edits | |||
| S May 25, 2014 at 12:06 | |||||
| Apr 10, 2013 at 10:57 | history | rollback | mata | Rollback to Revision 4 | |
| S Apr 10, 2013 at 10:51 | history | suggested | user2067316 | CC BY-SA 3.0 | covered all aspects |
| Apr 10, 2013 at 10:49 | review | Suggested edits | |||
| S Apr 10, 2013 at 10:51 | |||||
| Mar 6, 2013 at 19:24 | review | Suggested edits | |||
| Mar 6, 2013 at 19:28 | |||||
| S Aug 26, 2012 at 17:53 | history | suggested | qwertz | CC BY-SA 3.0 | made the table a bit more readable |
| Aug 26, 2012 at 17:45 | review | Suggested edits | |||
| S Aug 26, 2012 at 17:53 | |||||
| Oct 19, 2008 at 7:10 | history | edited | Tom Hawtin - tackline | CC BY-SA 2.5 | Added pre tag around table |
| Oct 19, 2008 at 0:56 | vote | accept | intrepion | ||
| Oct 18, 2008 at 20:25 | history | edited | David Segonds | CC BY-SA 2.5 | Added more information. |
| Oct 18, 2008 at 19:57 | history | answered | David Segonds | CC BY-SA 2.5 |