• Post Reply Bookmark Topic Watch Topic
  • New Topic

constructor  RSS feed

 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class A
{
A()
{
}
}
The class A can be referenced outside the package in which it is defined.
The class A cannot be instantiated outside the package in which it is defined.
The class A cannot be extended outside the package in which it is defined.
The class A can be referenced, instantiated or extended anywhere.
The above code will cause a compiler error. The constructors of public class have to be public.
Can anybody tell me the correct option's?.I think 1,2,3 and 4 are correct option's, please throw some light on it.
 
sonali saxena
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct ans are 1 and 4 as a public class can be referenced, instantiated and extended anywhere. hope this helps.
Sonali
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
the correct answers are 1 and 4
 
Junilu Lacar
Sheriff
Posts: 11429
174
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<pre>
public class A {
A() {}
}
</pre>

The class A can be referenced outside the package in which it is defined. TRUE because class A is public.
The class A cannot be instantiated outside the package in which it is defined. TRUE because constructor A() has default access (friendly/package), only classes in the same package will be able to create instances of class A
The class A cannot be extended outside the package in which it is defined. TRUE Even though class A can be referenced outside the package in which it is defined, it cannot be extended by any class outside the package because of the way the Java compiler inserts an implicit call to super() in a constructor. That implicit call to super() would fail if the class were in a different package.
The class A can be referenced, instantiated or extended anywhere.
FALSE see answers 2 & 3
The above code will cause a compiler error. The constructors of public class have to be public. FALSE all access specifiers are valid for constructors. Also, since constructors are not methods, you can even downgrade the accessibility of a constructor in a subclass, i.e. make it more restrictive than its superclass' constructor.
Junilu Lacar
(edited comments for #3...tricky)

[This message has been edited by JUNILU LACAR (edited April 28, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!