• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected and default constructor

 
Rashmi Tambe
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JQ+Question ID :958265097150
You can only call public and protected constructors of the super class in a subclass if the subclass is not in the same package because only those are inherited. True/False?
ANS: false
Explaination is really strange. But i dont understand why the statement is false. i tried following code. class c2 inherits from c1 which is in different package and it has one protected and one default constructor. While creating object of class c2 , compiler gives error...

The error is as follows...
c2.java:2: c1() has private access in pack.c1
{
^
c2.java:3: c1(int) is not public in pack.c1; cannot be accessed from outside pa
ckage
super(i);
^
2 errors
so i am confused :confused
could somebody explain this or i am interpreting the statement in some wrong way???
TIA
Rashmi
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Rashmi,
the statement is partially true but the reason being argued for that is false and so the answer is false.
it says "because only those are INHERITED..." now, constructors are never inherited. it's accesss is just determined by the access modifier when we call super() and depending upon that the access will be rejected or accepted.
regards
maulin.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic