• 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 doubt

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Vict{
private Vict(){//1
System.out.print("Vict");
}
}

public class BLBeck extends Vict{
public static void main(String argv[]){
new BLBeck();
}

BLBeck(){
System.out.print("BLBeck");
}
}
Here in //1 the construtor is marked as private .
But when i compiled it is giving an error

as constructor can be declared as private then why it is giving compilor error.

Also when we should use construtor as private to make it compile perfectly
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the compiler error is not "constructor can not be marked as private" in class Vict but "implicit super constructor is not visible" in class BLBeck.

Remember that when you declare a constructor without an excplicit call to a super constructor, an implicit call to super() is added by the compiler.

ie your code is equivalent to :
...
BLBeck(){
super(); // -> super constructor not visible.
System.out.print("BLBeck");
}
...

If your class contains only private constructors, it can only be instanciated from a static method in the class (Runtime.getRuntime()), and it cannot have subclasses...

If you don't want your class to be instanciated by others but want it to have subclasses, mark the constructor as protected.
[ September 29, 2005: Message edited by: Seb Mathe ]
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The error is :
-------------------------------------------------
BLBeck.java:12: Vict() has private access in Vict
BLBeck(){
^
1 error
-------------------------------------------------
Whenever you mark the only constructor of a class as private, that means you cannot instantiate that class.
Here when the constructor of BLBeck tries to access the super class constructor it finds that it is private and thus not visible is sub class.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the scope of a private modifier is
within the class so can it still inherit?
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Seb Mathe:

If your class contains only private constructors, it can only be instanciated from a static method in the class (Runtime.getRuntime()), and it cannot have subclasses...



[ September 29, 2005: Message edited by: Seb Mathe ]
 
Ranch Hand
Posts: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Originally posted by Seb Mathe:

If your class contains only private constructors, it can only be instanciated from a static method in the class (Runtime.getRuntime()), and it cannot have subclasses...





If you run the above code..an object is created since main resides in the same class as the class with the private constructor..


If you have another class...then..

(mind you we cannot extends a class that has only private constructors..)




Now if you run ....then you get the output....as

in GetObject
i is 10
in GetInstance
i is 10



In both the cases the object is created within the class that has the private constructor..

Note: This refers to the class that has only private constructors...

if there are overloaded public constructors then its different.

 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooops...

Thanks for correcting my mistake...
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a very gud xplaination Kumar!

Haritha
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey kumar...
that was real good explanation....
keep it up

:roll:
 
Jyothi Bhogadi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey kumar...
that was real good explanation....
keep it up

:roll:
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Karthik Rajashekaran:
class Vict{
private Vict(){//1
System.out.print("Vict");
}
}

public class BLBeck extends Vict{
public static void main(String argv[]){
new BLBeck();
}

BLBeck(){
System.out.print("BLBeck");
}
}
Here in //1 the construtor is marked as private .
But when i compiled it is giving an error

as constructor can be declared as private then why it is giving compilor error.

Also when we should use construtor as private to make it compile perfectly



As somebody said, the superconstructor is not visible when you call "super" because you have marked it private.

NO PRIVATE DATA/METHODS IS INHERITED or avaiiable outside the class even by the super keyword.

The thing is that, when you need your class to have subclasses, there should be atleast one constructor that is non-private and which you should "explicitly call" from your subclass unless that constructor is the no-argument constructor.

And if you want to instantiate the class,
a> from the same package> one no-modifier constructor or a constructor with greater access.
b> from elsewhere> public
c> subclass from outside the package> one protected or public

hope that helps.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic