Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance of abstract methods with package access

According section "8.1.1.1" of JLS we have:

A class C has abstract methods if either of the following is true:

• Any of the member methods (§8.2) of C - either declared or inherited - is abstract.

• Any of C's superclasses has an abstract method declared with package access, and there exists no method that overrides the abstract method from C or from a superclass of C

It is interesting, why do we have second option here. In particular, why do we have exactly "package access". And what about "public" or "protected" methods?

like image 301
Vitaly Avatar asked Oct 20 '22 05:10

Vitaly


1 Answers

From order of most private to most open, the java modifiers go:

  • private
  • package
  • protected
  • public

Child classes cannot inherit the package methods of a parent class in another package. So a class inheriting from a such a parent would not be abstract according to rule 1. Therefore the 2nd rule exists to address the situation where a child class inherits from an abstract parent and is unable to provide an implementation of the abstract package methods.

It's a nonsensical situation, and I would never expect to see this in any program anywhere. But the language must be fully specified or you could end up with a weird bug that allows a class with undefined methods to be instantiated.

like image 117
Dunes Avatar answered Oct 21 '22 20:10

Dunes