File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected constructor" Watch "protected constructor" New topic
Author

protected constructor

Saswata Sarkar
Greenhorn

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 {
SuperFoo(){
System.out.println("Constructor of superFoo runs ...");
}
}
*****************************************
package SubFoo;

import SuperFoo.SuperFoo;

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

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

On compilation these codes gave error
Source\SubFoo\SubFoo.java:5: 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.
Thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

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
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

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
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: protected constructor