• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protected consturctor VS public constructor

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic