Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Doubt regarding private contructors

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
this is a question that i came across in a mock test (source: www.jchq.net)..i am not able to understand why this code results in a compiler error..it states that since the constructor of the class Vict() is marked private, that couldnt be instantiated from outside classes. But then,I couldnt see any instantiation of class Vict in any outside classes..
according to me the answer should be "BLBeck".. Can anyone please advice and also let me know if my understanding is wrong?






Incorrect options are shown by strikethrough, any options you chose are marked by a tick
1 Compilation and output of "BLBeck"
2 Compilation and output of "BLBeckVict"
3 Compilation and output of "VictBLBeck"
4 Compile time error
You selected 1 option
The Correct Answer is
4) Compile time error
Note that the constructor for Vict is marked as private. A constructor can be marked as private, however if this is done an instance cannot be created from outside of that class via that constructor

Thanks in advance,
naveen

"Its the attitude and not the aptitude that determines the altitude!"

[Jesper Young: Fixed code tags]
[ July 14, 2008: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But then,I couldnt see any instantiation of class Vict in any outside classes..



Correct, you couldn't see the instantiation of class Vict, but when you instantiate a derived class BLBeck , its constructor gets called, which , in turn, calls its super class constructor and so on ,

SO , when as the constructor of "base" Vict class is private , you cant instantiate it , OR in other words you cant make an object of its derived class ,(without, explicitly calling its any non private constructor)!!

I am still learning this language, so any fellow ranchers ll explain in great details,

HTH,
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Sagar mentioned, the problem is that in the BLBeck constructor, an implicit call to super is being made, but since the super constructor Vict() is not visible, to avoid a compilation error, you must either make it visible by changing its access control level to default, protected or public, OR by explicitly invoking another constructor.

Mihai Fonoage
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

As Sagar and Mihai already stated...

An inherited class(sub class) always call the constructor of your super class. If you dont call it explicit, will be called implicit.

Having these classes:



Since we only have a private constructor in the super class (Vict) your sub class (BLBeck) will try to call implicit the constructor of your superclass, it should look like it:



So, since we will have a implicit call to the Vict constructor, and BLBeck cant see any constructors, it will give you a present, a compile time error.

I hope i could clarify your doubt, if not. Feel free to say and i'll try to make the point better.

Raphael Rabadan.
[ July 14, 2008: Message edited by: Raphael Rabadan ]
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that was a nice suggestion by Mihai. Thanks!

The following code runs well without any errors. The modifications are commeted.

 
Naveen Kumar G
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all, thanks a lot for your reply...


Raphael, sorry to disturb you again.. I understood the point that an implicit call to the base class constructor will be made by the sub class contructor during the compile time (using super()).


code:
--------------------------------------------------------------------------------

// BLBeck constructorBLBeck() { super(); // this line will be add implicitly.System.out.print("BLBeck");} // Vict consctructorprivate Vict() { super(); // Here we also have a implicit call to the super class constructor, Object's one.System.out.print("Vict");}

--------------------------------------------------------------------------------

But what is the purpose behind one more implicit call to the super class constructor, using super(), inside the base class constructor Vict() also?
In the comments, you have mentioned "...,Object's one".
Does it mean that this call is made by the compiler in order to initialize the instance variables,if any, of the Vict class?


Also, regarding the modified code provided by Milan, will the compiler implicitly insert the code "super();" in both the constructors of the Vict class?

Again, I thank everyone for answering my post with clear explanations!!!

regards,
naveen

"Its the attitude and not the aptitude that determines the altitude!"
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rancher ,

with constructors you can use private.
It is used for generally Singleton classes. This class should have
a static class which return the object of that class.

<blockquote>code:
<pre name="code" class="core">
class Vict{
private static Vict v = new Vict();

private Vict(){
}

public static Vict getInstance(){
return v ;
}
}

class VictTest{
public static void main(String ... args){
Vict objVict = Vict.getInstance();
}
}

</pre>
</blockquote>
It is one use to declare constructor private.
I hope this will help you.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Naveen Kumar G:

Does it mean that this call is made by the compiler in order to initialize the instance variables,if any, of the Vict class?
Yes, you can say this,


Also, regarding the modified code provided by Milan, will the compiler implicitly insert the code "super();" in both the constructors of the Vict class?

Yes, compiler implicitly calls to super class default constructor using super()

 
Naveen Kumar G
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

with constructors you can use private.
It is used for generally Singleton classes. This class should have
a static class which return the object of that class.


Madhukar,
Thanks for your explanation. It was very helpful..

Sagar,
okay, thanks for your reply..
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic