Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected constructor

 
neha. bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It allows the constructor of a subclass to call it. For example with a super() call.
 
neha. bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4385
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()");
}
}
 
neha. bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic