5

I understand why java doesn't allow to set weaker access privileges for overridden methods, but why is it the same for static methods? I mean, these methods only hide each others, right? So what's the problem from the encapsulation point of view?

P.S.

I know that there are 5 rules for hiding a method

  1. The method in the child class must have the same signature as the method in the parent class.
  2. The method in the child class must be at least as accessible or more accessible than the method in the parent class.
  3. The method in the child class may not throw a checked exception that is new or broader than the class of any exception thrown in the parent class method.
  4. If the method returns a value, it must be the same or a subclass of the method in the parent class, known as co-variant return types.
  5. The method defined in the child class must be marked as static if it is marked as static in the parent class (method hiding). Likewise, the method must not be marked as static in the child class if it is not marked as static in the parent class (method overriding).

But after all, I don't get the idea from the encapsulation prospective

vcmkrtchyan
  • 2,536
  • 5
  • 30
  • 59
  • 1
    Could you elaborate on what you mean by "weaker access privileges"? – callyalater Mar 01 '16 at 16:52
  • When the method with the same signature has an access modifier with less access than a method in the base class with the same name – vcmkrtchyan Mar 01 '16 at 16:54
  • Meaning a visibility modifier? (`public`, `protected`, &c.) – callyalater Mar 01 '16 at 16:55
  • I think it is due to the definition of access control : "The Java programming language provides mechanisms for access control, to prevent the users of a package or class from depending on unnecessary details of the implementation of that package or class. If access is permitted, then the accessed entity is said to be accessible." --- While hiding if code doesn't follow same access control rules it could break the semantic relationship. https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6 – kosa Mar 01 '16 at 16:55
  • @Carmine Are you implying that you can override static methods? – Laf Mar 01 '16 at 16:57
  • @Laf no, i just want to know why you can't set weaker privileges to a method that hides another, because that doesn't seem to be a breach in the security, is it? – vcmkrtchyan Mar 01 '16 at 16:58
  • Isn't that a duplicate of this one ? http://stackoverflow.com/questions/34365337/why-does-java-restrict-the-access-modifier-of-a-hiding-method – Bax Mar 01 '16 at 17:06
  • Looks line the question was already asked and answered http://stackoverflow.com/questions/26963828/reducing-the-visibility-of-a-static-method In short "the fact that a static method can be hidden has nothing to do with changing the accessibility of the method." – Andriy Kryvtsun Mar 01 '16 at 17:20

2 Answers2

1

The same rules are valid and for method hiding https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.3

Andriy Kryvtsun
  • 3,220
  • 3
  • 27
  • 41
  • As i mentioned in the question, I do know the rules, I just want to understand a specific point of it. What was the reason to not allow a method having a weaker access privilege than it's hidden version? – vcmkrtchyan Mar 01 '16 at 16:59
  • let's say java would allow that for hiding a method, what would be then? I know what would be bad for overriding, but there's no an issue like that in hiding, is there? – vcmkrtchyan Mar 01 '16 at 17:00
0

Static methods have special access permissions that allow friend access to members within a instance. For example, you could have a static method called create that creates a default instance of the class and directly modifies data within the instance without using instance properties or instance methods (just for a usage example, not necessarily a usage recommendation).

Because of this special access, lower access permissions to the static methods could allow you to create an object in a state that you could not otherwise use, or modify an object in an unpredictable and non-encapsulated way.

This is only a minor annoyance for most user-defined classes, but allowing this when sub-classing e.g. containers could expose serious undefined behavior problems.

Matt Jordan
  • 2,133
  • 9
  • 10
  • Could you sketch out an example of such a problematic scenario? – biziclop Mar 01 '16 at 17:01
  • 2
    Yes, please provide an example. I've never heard of a static method having different permissions than a non-static method has. – VGR Mar 01 '16 at 17:13