• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes
the correct answers are 1 and 4
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic