-1

Note: Please read the whole thing before you wrongly mark it as a duplicate.

So I'm not sure if this is write and I can't find an easy chart to answer it: -Is it true that if you have a PUBLIC, PROTECTED, OR PACKAGE PRIVATE method or field in the superclass and the subclass is in the SAME PACKAGE, then a class OUTSIDE OR WITHIN the package can access those methods or fields via a new subclass object and if the field or method is PRIVATE, then it CANNOT? -Is it true that if you have a PUBLIC, OR PROTECTED method or field in the superclass and the subclass is in a DIFFERENT PACKAGE, then a class OUTSIDE OR WITHIN the package can access those methods or fields via a new subclass object and if the field or method is PACKAGE PRIVATE, OR PRIVATE then it cannot?

In other words, all the access combinations (32 combinations) of: public, protected, package private, and private for methods, fields, for subclasses in same, or different package, access by a class creating the subclass object in a class in the same or different package. Note the main difference between this question and the "duplicates" is that I am wondering about access with NON-SUBCLASSES within/outside the package. NOT JUST THE STUFF INSIDE THE SUBCLASS. The duplicates are not specific enough because there are tiny rules that are not as simple as a 4 x 2 table. Example, for protected, a subclass outside of the package cannot run a super class method on a superclass object, only the subclass object. Little things like that are not answered in the "duplicates".

And can y'all read the whole dang thing before you mark it as a duplicate. I explained why it wasn't but lazies don't read the whole thing nor answer the question.

gmanrocks
  • 221
  • 1
  • 12
  • Last part edited for clarity on how it is not a duplicate – gmanrocks Mar 04 '19 at 20:00
  • The 4x5 table in the duplicate covers every scenario that can possibly occur with access modifiers. – jaco0646 Mar 04 '19 at 20:12
  • *"Example, for protected, a subclass outside of the package cannot run a super class method on a superclass object, only the subclass object."* A sub class in a different package _can_ call parent protected methods. Do you confuse "protected" and "default"? – Tom Mar 04 '19 at 20:13
  • @Tom I do not confused "protected" and "default" You class Foo in package A. You have class Bar extends Foo in Package B. You create new Foo in class bar. Foo f = new Foo(); You cannot run a protected method from class Foo using the f object. – gmanrocks Mar 04 '19 at 20:20
  • @Tom you can create a Bar object b inside the Bar Class and run a Foo method from it though. – gmanrocks Mar 04 '19 at 20:22
  • If you think you are asking a question not covered by the linked one, can you give an example to clarify? – Nivas Mar 04 '19 at 20:26
  • "And can y'all read the whole dang thing before you mark it as a duplicate. I explained why it wasn't but lazies don't read the whole thing nor answer the question." - Dunning Kruger effect in action. – Ben R. Mar 15 '19 at 09:05
  • @BenR. Yup, duplicate marker-ers definitely experiencing that. ;) – gmanrocks Mar 15 '19 at 14:23

1 Answers1

0

I found this table on the docs:

                  Access Levels

Modifier    Class Package Subclass World
public        Y      Y        Y      Y
protected     Y      Y        Y      N
no modifier   Y      Y        N      N
private       Y      N        N      N


Based off of my experience:

public: any class can access
protected: only classes in the same package or that inherit the class can access
private: only the class can access. Subclasses cannot access private fields/methods of their parent.

Docs

Benjamin Urquhart
  • 1,566
  • 9
  • 17
  • Lets look at protected for example. You have a class SuperClass in Package A, and public class SubClass in Package B that extends the superclass. Let us say we have a protected method called hello() in the superclass. The chart says the access level includes subclasses. So yeah you can create in the subclass, SubClass sc = new Subclass; and do sc.hello(). That would work and that follows the chart but for example even though hello in super is protected, you can do SuperClass s = new SuperClass(). But you cannot do s.hello() The chart is ambiguous. Even though is says accessible via sub. @nivas – gmanrocks Mar 04 '19 at 20:32
  • Last I checked you can't `protect` a class. You can only make it public, private, or leave no modifier. – Benjamin Urquhart Mar 04 '19 at 20:34
  • Sowwy I mean method. Edited. @BenjaminUrquhart – gmanrocks Mar 04 '19 at 20:37
  • Yes, because SuperClass is not an instance of SubClass. I don't have anything to back up that statement other than observation – Benjamin Urquhart Mar 04 '19 at 20:44
  • Yet hello() is in the superclass and protected allowing extension. See it is ambiguous and fine details like that were what I was looking for but it is fine. – gmanrocks Mar 04 '19 at 21:01