Is it possible to extend a class with no constructors in Java?

11,274

Solution 1

A class with no constructors has an implicit public no-argument constructor and yes, as long as it's not final, it can be sub-classed.

If the class has only private constructors then no, it can't.

Solution 2

Question has been answered, but to add a comment. This is often a good time to propose that code be written to be somewhat testable.

Don't be a pain about it, research what it takes (probably Dependency Injection at least), learn about writing mocks and propose a reasonable set of guidelines that will allow classes to be more useful.

We just had to re-write a bunch of singletons to use DI instead because singletons are notoriously hard to mock.

This may not go over well, but some level of coding for testability is standard in most professional shops.

Solution 3

Yes, you can mock the object, although it may not be possible to subclass it (certainly not without getting very intimate with the class loader, anyway). Here is how you do it with JMock.

Mocking in this fashion allows you to keep the type, without subclassing though it will probably quite hard to impossible to tease out only certain behavior. So this method is appropriate for testing classes that use this class, not for testing the class itself.

If you actually have access to the source code of the class, you could implement an inner class which allows you to extend it, although if you could do that, you could just make one of the constructors package private as well.

There are also dynamic languages which will allow you to do the subclassing, and implement a Java interface which the Java code can interact with, but I'm not that familiar with the details.

Solution 4

If java class doesn't have any defined constructors then there is no problem for you. The problems will be if class will have any constructors defined and all of them will be invisible for you (private).

Solution 5

You can change the visibility modifiers via reflection. Here is an article listing how.

Share:
11,274
Ben S
Author by

Ben S

Mobile Software Engineering Manager at Square currently working on Cash App iOS with a University of Waterloo bachelor's degree in Computer Science, Software Engineering Option. Previous experience at Google, Amazon.com, OpenText, Research In Motion, Sybase and Bridgewater Systems.

Updated on June 04, 2022

Comments

  • Ben S
    Ben S about 2 years

    For unit testing purposes I'm trying to write a mock object of a class with no constructors.

    Is this even possible in Java, of is the class simply not extensible?

  • Ben S
    Ben S about 15 years
    The class in question has only private constructors, including the no-argument one, so I guess I'm just out of luck.
  • mP.
    mP. about 15 years
    Looks like that class isnt very friendly in terms of testing. You really should be using interfaces rather than concerete types...
  • Eddie
    Eddie about 15 years
    But this won't help you extend the class. The compiler won't let you extend a class with only private constructors.
  • matt b
    matt b about 15 years
    So then you're testing a singleton? This always turns out to be a pain in the butt.
  • jdfhf
    jdfhf about 15 years
    If the class has interfaces then you can mock the interfaces.You could also work around a final class creating a new class that implements the interfaces and encapsulates a copy of the final class to be extended.
  • Ben S
    Ben S about 15 years
    Yes, the object I'm trying to mock is a singleton who's only inheritance comes from Object and has no interfaces...
  • Vishy
    Vishy about 15 years
    Technically inner/nested classes can use private members of the enclosing class and it can extend it even if it only have private constructors. However, I am guess this won't help the OP as this assumes you can modify the source of the original class, in which case he would just make the constructor non-private.
  • Ben S
    Ben S over 14 years
    The class I was looking to mock was a Blackberry class which I have no control over.
  • hotshot309
    hotshot309 over 11 years
    You make a good point (assuming one is mocking code that they can change), but this probably should have been entered as a comment instead of an answer.
  • Bill K
    Bill K over 11 years
    I agree but the inability to separate text into paragraphs in comments often leaves me frusterated, but these days (three years later) I think I would have done exaclty that--live and learn.