This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected constructor" Watch "protected constructor" New topic

protected constructor

Saswata Sarkar

Joined: Jun 13, 2009
Posts: 5
Dear Ranchers,

While experimenting with Java codes I came across something I'd like to discuss...

package SuperFoo;

public class SuperFoo {
System.out.println("Constructor of superFoo runs ...");
package SubFoo;

import SuperFoo.SuperFoo;

public class SubFoo extends SuperFoo {
System.out.println("This Constructor runs :" );

public static void main(String[] args){
SubFoo sf = new SubFoo();

On compilation these codes gave error
Source\SubFoo\ SuperFoo() is not public in SuperFoo.SuperFoo; cannot be accessed from outside package
public class SubFoo extends SuperFoo {
1 error

The codes compile and run if the constructor declaration in SuperFoo is changed to "protected SuperFoo".

I thought out that,
a) Since the packages differ
b) Since constructor is a member too
c) Since subclasses outside the package can access public /protected members of Superclass in a different package through inheritence

I thought this may be the way to sort out the problem..

Members, please tell me whether my assumption is right or wrong.
Ankit Garg

Joined: Aug 03, 2008
Posts: 9405

Well I'm not sure what is the problem you are facing exactly, but if you want your class to be subclassed outside of its package, its constructor has to be public or protected (as you said yourself). If the constructor is protected, your class can be subclassed outside of its package but an instance of your class cannot be created outside of its package...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg

Joined: Aug 03, 2008
Posts: 9405

Also please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it...
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Cannot understand your problem,

But can package name and class name be same ??? Are you sure about it ???

I think the package name and class name should be different !!!

Thanks !!!

SCJP 6.0 96%

(Connecting the Dots ....)
Matthew Brown

Joined: Apr 06, 2010
Posts: 4541

Sahil Kapoor wrote:But can package name and class name be same ??? Are you sure about it ???

I think the package name and class name should be different !!!

They can be - there's nothing to stop it. They generally shouldn't be, because they have different preferred naming conventions. Packages ought to be all lower case, and classes should start with a capital. But that's just convention - the language doesn't force it.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: protected constructor
It's not a secret anymore!