File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java 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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "protected constructor" Watch "protected constructor" New topic
Author

protected constructor

neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31

J.java:4: error: I() has protected access in I
I obj=new I();
^
1 error

Then what is the use of protected access modifier for constructor ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

It allows the constructor of a subclass to call it. For example with a super() call.
neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31
Matthew Brown wrote:It allows the constructor of a subclass to call it. For example with a super() call.

Could you give me an example ?
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

package pkg;
public class I{
protected I(){
System.out.println("I()");
}
}
import pkg.I;
class J extends I
{
public J()
{
super();
System.out.println("J()");
}
}


SCJP
Visit my download page
neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31
J extends I and I() is protected constructor why it won't be available in class J while protected class members of I() are available.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

neha. bajaj wrote:
Matthew Brown wrote:It allows the constructor of a subclass to call it. For example with a super() call.

Could you give me an example ?


Wasn't that an example?

Henry
neha. bajaj
Ranch Hand

Joined: Jul 15, 2012
Posts: 31
Henry Wong wrote:
neha. bajaj wrote:
Matthew Brown wrote:It allows the constructor of a subclass to call it. For example with a super() call.

Could you give me an example ?


Wasn't that an example?

Henry


Sorry i was writing that post while he has been edited his post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected constructor