wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected constructors 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 constructors" Watch "protected constructors" New topic
Author

protected constructors

Saith Govind
Greenhorn

Joined: Jan 24, 2003
Posts: 13
Can anyone explain this:
"It is an error for a protected constructor to be invoked from another package, even if the type is a subclass of the type defining the constructor"
If there is a private default constructor in superclass, what happens when subclass's default constructor is invoked? will the superclass's default private constructor is triggered?
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
Let me try to answer your questions.

The above code comiles and runs fine. But if you remove the comments from //C1 c1= new C1(); you get compiler error.
For your second part as you are mentioning about private default c'tor -- your class can't be top level class and it should be inner class.
So here is the code that runs fine. I strongly believe that you mean by no arg c'tor than default c'tor

[ March 14, 2003: Message edited by: Sarma Lolla ]
[ March 14, 2003: Message edited by: Sarma Lolla ]
[ March 14, 2003: Message edited by: Sarma Lolla ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Okay, let's take this one question at a time...

Can anyone explain this:
"It is an error for a protected constructor to be invoked from another package, even if the type is a subclass of the type defining the constructor"

This simply means that you can't explicitly invoke a protected constructor of a class outside that package, even if the class you're invoking it from extends that class. Let's look at an example:

As you can see from this example, we can easily invoke both public and protected constructors within the same package. However, let's create a class that extends SuperClass from outside the package and see what happens...

In this case, we have a compiler error on line 2. You see, we don't have access to the protected constructor of SuperClass because we're outside the package. Normally, when a class extends another class, it gains access to all protected methods, but constructors are not inherited the same way methods are. Therefore, line 2 is a compiler error. If we comment out line 2, however, you get the following output:

Notice that the protected constructor of SuperClass was invoked implicitly when we created a new instance of SubClass. That's because the parent must be instantiated prior to the child.
Okay, so that takes care of protected constructors, now, let's look at private constructors...

If there is a private default constructor in superclass, what happens when subclass's default constructor is invoked? will the superclass's default private constructor is triggered?

Well, let's try it out by modifying our classes slightly:

Notice, in this case, we're simply trying to create an instance of the subclass - the subclass is even in the same package as the superclass. However, if you tried to compile this code, you'd get the following compiler message:

SubClassInSamePackage.java:3: SuperClass() has private access in superclass.SuperClass

In such a case, the compiler complains because, according to the code (the private constructor), that method can never be invoked outside that class, not even by a child class.
I sure hope that helps,
Corey


SCJP Tipline, etc.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I am quotting from JLS because it is very suitable for this example:

6.6.2.2 Qualified Access to a protected Constructor
Let C be the class in which a protected constructor is declared and let S be the innermost class in whose declaration the use of the protected constructor occurs. Then:
* If the access is by a superclass constructor invocation super(. . .) or by a qualified superclass constructor invocation of the form E.super(. . .), where E is a Primary expression, then the access is permitted.
* If the access is by an anonymous class instance creation expression of the form new C(. . .){...} or by a qualified class instance creation expression of the form E.new C(. . .){...}, where E is a Primary expression, then the access is permitted.
* Otherwise, if the access is by a simple class instance creation expression of the form new C(. . .) or by a qualified class instance creation expression of the form E.new C(. . .), where E is a Primary expression, then the access is not permitted. A protected constructor can be accessed by a class instance creation expression (that does not declare an anonymous class) only from within the package in which it is defined.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected constructors