• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

private constructors

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends
I had some problem regarding the private constructor and having the final class. according to Robert Heller the Math class is final (so that it cannot be subclassed) and the constructor is private(so that it cannot have instance).
Now the Problem is that i was trying to subclass a class whose constructor is private. it was not working as showing error that constructor is declared private.
Programme is as follow: file name B.java

class A
{
private A(){}
}
class B extends A
{
}
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
A constructor will call the no arguments constructor in the superclass, so you'll get the compile error because dont have access to it.
You didn't define any constructors in B, but the compiler will still generate one.
This code will compile as you explicitly call another (non private) constructor:
class A{
private A(){}
A(int i){}
}
public class B extends A{
B(){
super(2);
}
}
Hope it helps
[This message has been edited by Mafalda Alabort (edited March 21, 2001).]
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi bindu
what's i think is happenning is that when u compile class B, u have not declared a constructor, so the compiler declares a default constructor, like, b(){super();}, it calls super() to initialize the superclass, but u have one private constructor, so the super(); call refers to that constructor, but that contructor is private, so u get an error.
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bindu,
in my opinion...,to answer this question, first we have to understand why java allowed constructor to be declare private.
The reason why this constructor is private is some programmers might want to control how an object is created and prevent someone from directly accessing a particular constructor, because "private" is accessible only to the class which defines it.
So user can not create this object via it's constructor.
But usualy the programmers define a static method for the user to create an object of that class.
for example :
here I changed you code to make it work...
-----------------------------------------------
class A{
private A(){}
public static A makeA() {return new A();}
}
public class B {
public static void main(String[] args) {
A a = A.makeA();
}
}
-----------------------------------------------
it works...!,
BUT this works only with COMPOSITION, not inheritance, as you see, i did not use keyword "extends A" in class B, because if you use extends, this code dosen't work.
because one of inheritance side effect is :
"Java automatically inserts calls to the base-class constructor in the derived-class constructor - either using default constructor or using super keyword".
that's why when you extend to the Base class using "class B extends A", it won't work, because the derived class can not call the base class constructor (again, it's private).
Hope this Help
stevie
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, here is the bonus :
according to my post,
that's why math class is FINAL (it can not have sub-class), and why there is a bounce of static method in Math class.
thank you
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we have static contructors or in short what r the possible modifiers for the constructors.
thanks in advance.
rahul
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you can not have static constructors. A constructor's purpose is to create an instance of the class, static means it relates to the WHOLE class not an instance.
A constructor can have any level of access specifier (coderanch, protected, friendly, private).


8.8.3 Constructor Modifiers

ConstructorModifiers:
ConstructorModifier
ConstructorModifiers ConstructorModifier
ConstructorModifier: one of
public protected private
The access modifiers coderanch, protected, and private are discussed in �6.6. A compile-time error occurs if the same modifier appears more than once in a
constructor declaration, or if a constructor declaration has more than one of the access modifiers coderanch, protected, and private.
Unlike methods, a constructor cannot be abstract, static, final, native, strictfp, or synchronized. A constructor is not inherited, so there is no need to
declare it final and an abstract constructor could never be implemented. A constructor is always invoked with respect to an object, so it makes no sense for a
constructor to be static. There is no practical need for a constructor to be synchronized, because it would lock the object under construction, which is normally
not made available to other threads until all constructors for the object have completed their work. The lack of native constructors is an arbitrary language design
choice that makes it easy for an implementation of the Java virtual machine to verify that superclass constructors are always properly invoked during object creation.
Note that a ConstructorModifier cannot be declared strictfp. This difference in the definitions for ConstructorModifier and MethodModifier (�8.4.3) is an
intentional language design choice; it effectively ensures that a constructor is FP-strict (�15.4) if and only if its class is FP-strict.

 
Rahul Pawar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks cindy ,
thanks for ur kind responce
Rahul
reply
    Bookmark Topic Watch Topic
  • New Topic