File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected and default constructor 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 and default constructor" Watch "protected and default constructor" New topic
Author

protected and default constructor

Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
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

Joined: Nov 04, 2001
Posts: 1871
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected and default constructor
 
Similar Threads
access problems
protected member
Garbage collection doubt
protected constructors
how many objects will be eligible for garbage collection