• 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

Abstract class with private constructor (why compiles if I can't extend)?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What can I do or how do I use an abstract class with private constructor?

This code compiles.



If I can't extend this class (because the private constructor) why it compiles?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pedro abs wrote:What can I do or how do I use an abstract class with private constructor?

This code compiles.



If I can't extend this class (because the private constructor) why it compiles?



It compiles fine because there are no compilation issues there- It confirms to the language specifications.

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

It compiles fine because there are no compilation issues there- It confirms to the language specifications.



Ok, I know it compiles because I've tested, but .... WHAT CAN I DO WITH THIS KIND OF CLASS IF IT WAS DECLARED ABSTRACT AND I CAN'T EXTEND BECAUSE IT'S CONSTRUCTOR IS PRIVATE?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pedro,
This code compilation will be fine unless you try to initialize the class which is extending Clown.
Constructor can be private, so the compiler is just following the fact.
But if you try to create any 'new' Clown, compiler will get angry and will burst out as the constructor of Clown is 'private'.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pedro abs wrote:

It compiles fine because there are no compilation issues there- It confirms to the language specifications.



Ok, I know it compiles because I've tested, but .... WHAT CAN I DO WITH THIS KIND OF CLASS IF IT WAS DECLARED ABSTRACT AND I CAN'T EXTEND BECAUSE IT'S CONSTRUCTOR IS PRIVATE?



I dont know what can be done for this. Either you make the constructor as public (if you have access to the Abstract class) or the provider of the class might want the implementation to be that way.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruchir Verma wrote:Hi Pedro,
But if you try to create any 'new' Clown, compiler will get angry and will burst out as the constructor of Clown is 'private'.


Its an Abstract class- It cannot be instantiated. And you cannot extend it as its constructor is private.
 
pedro abs
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok my friends. Thank you everybody ! I just asked because it didn't make sense for me !!! Some things in the exam doesn't make sense to but we have to know when it's ok or not ...

thank you again \!!!
 
Ruchir Verma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly...
You can neither extend nor instantiate Clown, it means the class Clown is of no use.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pedro abs wrote:Ok my friends. Thank you everybody ! I just asked because it didn't make sense for me !!! Some things in the exam doesn't make sense to but we have to know when it's ok or not ...

thank you again \!!!


Most of the questions on the exam are to test the Language feature, rules and so on. One cannot compare with the real situations all the time. All the best
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your case the class is actually useless but an abstract class with a private constructor can have uses. What if you add a factory method to the class which returns instances of the class?? Then the class would be useful. Since the compiler cannot check whether its possible to instantiate a class or not (i.e. look for factory methods), so the code is allowed as it doesn't break any rules...
 
pedro abs
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:In your case the class is actually useless but an abstract class with a private constructor can have uses. What if you add a factory method to the class which returns instances of the class?? Then the class would be useful. Since the compiler cannot check whether its possible to instantiate a class or not (i.e. look for factory methods), so the code is allowed as it doesn't break any rules...



Ok, I agree BUT ... even in a factory method I can't instantiate the class because ... remember .... it's abstract ! And I can't do:

new Clown();

And I can't extend it because it's constructor is private .... so ...... I can't do nothing with this "clown" code !
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example using a factory method....




As you can see, having only private constructors doesn't mean that the class can't be extended.

Henry
 
pedro abs
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Henry, thank you for your example !!!
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another Example using anonymous classes :


The output is:
LOL
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic