2

Say I have a class declared with default access. If a member in the class is declared protected or public, is it equivalent to the member being declared with default access?

For example:

In CarA.java:

class CarA { //class declared with default access
   public int odometer;
   protected void forward(int distance){...
   }
   protected static void tally(){...
   }
}

In CarB.java:

class CarB { //class declared with default access
   int odometer;
   void forward(int distance){...
   }
   static void tally(){...
   }
}

Are CarA and CarB equivalent?

My reasoning is that since the class is not declared as public, the class is not accessible outside of its package, so its members should not be accessible outside of its package as well. Could someone confirm my thinking is correct?

flow2k
  • 3,999
  • 40
  • 55
  • See this, http://stackoverflow.com/questions/215497/in-java-difference-between-default-public-protected-and-private – wannadream May 10 '17 at 04:37
  • i believe you'll need to [read this QA](http://stackoverflow.com/questions/267781/java-class-accessibility). its no different on method visibility and class visibility.. afaik. – Bagus Tesa May 10 '17 at 04:37
  • 1
    It appears these resources address the access for class and member separately, but not in the combination I indicated above. – flow2k May 10 '17 at 04:45
  • @BagusTesa How would you access a public member from outside the package if the class has default (package-private) access? – flow2k May 10 '17 at 04:46

3 Answers3

3

No, they're not equivalent. Define these in the same package:

public class CarC extends CarA { }

public class CarD extends CarB { }

And in a different package:

import your.package.CarC;
import your.package.CarD;
public class NewClass
{
    public static void tryThis(CarC c, CarD d) {
        int n1 = c.odometer;  // legal
        int n2 = d.odometer;  // illegal
    }
}
ajb
  • 31,309
  • 3
  • 58
  • 84
2

No, these would not be equivalent. The odometer would almost be equivalently privileged*, the methods would not be.

This is because what you're calling "default access", also referred to as package-private, means that other members of that package can access the object or method in question. So where you've made odometer public, it would essentially be available to anything with access to the parent class, the protected status of the methods would be available to the package and classes that extend the class in question, even if they are outside of the package, unlike package-private.

*The public odometer in CarA could be accessed by an object or method outside of this package by routing it through a public class that extends this class whereas the package-private odometer in CarB would always be package-private. This is a great reason why one should avoid writing code like that, other developers might assume that the variable is not publicly available.

Glen Pierce
  • 4,401
  • 5
  • 31
  • 50
  • 1
    `odometer` isn't quite equivalent either--see my answer. – ajb May 10 '17 at 05:01
  • Good catch. I hadn't thought of sneaking through a public class. – Glen Pierce May 10 '17 at 05:06
  • 1
    Glen Pierce, you say "the protected status of the methods would only be available to classes that extend the class in question, not anything inside the package". If I understand correctly, `protected` members are visible anywhere inside the same package, including non-subclasses. – flow2k May 10 '17 at 05:37
  • @flow2k you are correct, I have edited my answer. Thanks :) – Glen Pierce May 10 '17 at 05:43
0

As far as I know, you can access the members of the class when you can create an instance of the class or when you inherit the class.

A default class can not be accessed outside the package. So I think you can not inherit it as well(outside the package). So I think they are pretty much the same.

You can access the protected members of the class when you can access the class itself(CarA).

Hope I'm right and this gives you some idea.

Abhishek
  • 1
  • 4