Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

private constructors

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Quize site
Question 46.
A class which has all its constructors declared as private
1) Cannot be instantiated by any other class.
2) Cannot be extended.
3) Both i and ii.
4) has to be declared final.
the answer is 3.
But what is wrong with:
public class A
{
private A()
{}
public static class subA extends A
{}
}
As I understand subA ia a child of A and it works perfect.
Does it mean that class 'A' CAN BE extended?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Itcannot be extended as the sub class will not be able to call the constructor for the base class.
Atleast that is what my explaination is
 
yanish
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, what about that ?
public class A
{
private A()
{}
public static class subA extends A
{
public subA()
{
super();
}
}
}
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMO, since subA is an inner class defined in A, so subA HAS access to the private constructor of A, so the program compiles, and runs. If subA was not an inner class, and extended A, then it might encounter an error. I've not tested it as yet.
Any way, both of ur programs are similar. Java implicitly creates public subA() and places the statement super() in the first code written by you.
Thanks,
Aman
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic