• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected constructor

 
Saswata Sarkar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic