• Post Reply Bookmark Topic Watch Topic
  • New Topic

protected consturctor VS public constructor  RSS feed

 
Cainiao Zou
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, please look at this sample code:



the consturctor of A must be public or protected, if it's private, there is an error in B(), because the call of super() not visible. At this point there is no different between protected consturctor and public constructor

now let's look at the constructor of B, it can be public, protected, private, default... no matter which access level it is, the new B() will call the constuctor! So here has still no differnent.

So is there any different betwenn protected consturctor and public constructor at all?
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turn the B constructor to protected and then try to recompile your Test class. You'll see the difference.
 
Cainiao Zou
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Turn the B constructor to protected and then try to recompile your Test class. You'll see the difference.


aha, i see.
with new B() can JVM call all type constructors, but the constructor must be visible at that point.
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid I don't see. I would expect public, protected, and default to work for the constructors of both A and B, and I would expect private to fail to compile for each. That's exactly the behavior I get in testing. What am I not seeing?
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If B has a protected constructor, then that constructor can be accessed by classes which are subclasses of B and by classes which are in the same package as B. Did your testing cover all of the possibilities implied by that?
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:and by classes which are in the same package as B.

Which actually makes my example a bad one - class Test is in the same package as class B (the default one)...
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also some technical verbiage in the definition of "protected" which makes overriding a protected method of class X different than calling a protected method of an object of class X. For example the clone() method of Object is protected; if you subclass Object then you can override it, but if you have a reference to an Object then you can't call it. However since constructors don't override anything in their superclass, I'm not sure how this rule applies to the use of super() in a constructor.
 
Ed Connery
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:If B has a protected constructor, then that constructor can be accessed by classes which are subclasses of B and by classes which are in the same package as B. Did your testing cover all of the possibilities implied by that?
Actually, my testing covered what I (thought I) knew worked under the "package and kids" accessibility rules. It didn't cover what I expected would fail, so the testing was distinctly incomplete.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!