Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't java allow to assign weaker access privileges to static methods in a child class?

Tags:

java

oop

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

like image 385
Carmine Avatar asked Nov 09 '22 19:11

Carmine


1 Answers

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

like image 199
Andriy Kryvtsun Avatar answered Nov 14 '22 21:11

Andriy Kryvtsun