Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 705
Ben S Avatar asked Jun 17 '09 00:06

Ben S


People also ask

Can you have a class without constructors Java?

Java doesn't require a constructor when we create a class. However, it's important to know what happens under the hood when no constructors are explicitly defined. The compiler automatically provides a public no-argument constructor for any class without constructors. This is called the default constructor.

Can any class in Java extend itself?

A class cannot extend itself since it IS itself, so it is not a subclass. Inner classes are allowed to extend the outer class because those are two different classes.

Can I extend class with private constructor?

The answer is you can't extend the Parent class if it has a private default constructor. You have to make the constructor available to the subclass. In this case you need to have a default constructor that have a protected or public or default access modifier.

Can we extend constructor in Java?

It is never possible. Constructor Overriding is never possible in Java. This is because, Constructor looks like a method but name should be as class name and no return value.


6 Answers

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.

like image 98
Steve Reed Avatar answered Sep 26 '22 05:09

Steve Reed


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.

like image 43
Bill K Avatar answered Sep 28 '22 05:09

Bill K


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.

like image 36
Yishai Avatar answered Sep 27 '22 05:09

Yishai


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).

like image 20
okutane Avatar answered Sep 27 '22 05:09

okutane


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

like image 44
javamonkey79 Avatar answered Sep 28 '22 05:09

javamonkey79


You can mock the class, and any or all of its constructors, using JMockit.

It's a mocking toolkit for Java which lets you mock just about anything. Even if the class is non-public and/or nested, it can still be mocked. There are several mechanisms in JMockit that can be used for that. The toolkit distribution contains lots of sample JUnit tests as well.

If the class implements some interface or extends an abstract class, you can tell JMockit to "capture" and mock implementations of the base type on demand, as they are loaded by the JVM, and even assign the created instances to a field in the test class.

like image 42
Rogério Avatar answered Sep 28 '22 05:09

Rogério