• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is default top-level classes default constructor's acces type ?

 
Myke Enriq
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.9

The link claims that a default constructor's access modifier is that of the class. However I think it may ojnly be talking about inner classes.

As for:
package Aa;
class A{}

package Bb;
import Aa.A;
class B extends A {}

- this code compiles just fine (although it should not ?)
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a small notation below the image and it says why the constructor of inner class in accessible.

By the way the code you've written should compile fine since class B extends A and the constructor access modifier by default is protected.
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Myke Enriq wrote:http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.9

The link claims that a default constructor's access modifier is that of the class. However I think it may ojnly be talking about inner classes.

As for:
package Aa;
class A{}

package Bb;
import Aa.A;
class B extends A {}

- this code compiles just fine (although it should not ?)


Hi Myke,

You are correct this code should not compile. I put this code on my system and it indeed does not compile.

The errors I get are pasted below:


Do you have a folder called Aa with class A in it? Do you have a different folder called Bb with class B in it?

Compiling these classes in the folder they are in is possible but at run time they will need to be accessed from outside of the package so the command will be like
Compiling the code in your example should only work for class A (this class should also run but do absolutely nothing).

Class B should not compile because default classes are indeed not accessible outside of the package they are declared in even if there is an import statement.

Also, yes to my understanding constructor's are inherited from the class definition unless explicitly given a different access modifier. So in your example both class A and class B would have default constructors since these classes are defined with the default access modifier, which is the absence of an access modifier.

Interesting question. Thanks for posting. Keep learning.

Respectfully,

Ted
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I didn't read carefully.
The access modifier is default and any class can access it as long as it is same package.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic