Java - private constructor versus final and more

Suppose a class exists with all its constructors declared private.

For instance:.

public class This { private This () { } public someMethod( ){ // something here } // some more-- no other constructors } 

From what I know, creating all private constructors is similar to declaring the "This" class as final - so that it cannot be extended.

However, the Eclipse messages that I receive give me the impression that this is possible - you can extend the extension of the all-constructors-private class. Take a look at this:

When I try to extend this class with something like

 public class That extends This { ... } 

Eclipse gives me an error: "The implicit super constructor This () is not displayed for the default constructor. An explicit constructor must be defined.

When I define my own constructor:

 public class That extends This { That () {..} ... } 

this time I get: β€œThe implicit super constructor This () is not displayed for the default constructor. Must explicitly reference another constructor.

Is there a way around this - extending a class from which all constructors are private?

if so, how?

If not, what's the difference between stopping the class from expanding by i.) Making its constructors private, and ii.) Defining it as final ?

Note: I have seen. Can a constructor in Java be private? among some other discussions.

+2
source share
3 answers

A class with private constructors cannot be created, except for the form inside the same class. This makes it useless (perhaps, but not compiled) for extending it from the antoher class.

This does not mean that it cannot be a subclass at all, for example, among inner classes that you can extend and call a private constructor.

+1
source

You declare the final class against your private constructor for various reasons:

  • You are making a final class to indicate that the class is not intended to be inherited.
  • You create all private constructors to give the class control over its instance.

In other words, using final controls inheritance, while using private constructors controls instantiation.

Note that declaring private constructors disables inheritance only externally. Inside a class, you can still inherit it using a name or an anonymous derived class.

When you create all the constructors of the private class, you need a static method that is publicly available to make the class usable. One common method of the static method is the factory method: you can allow class users to invoke private constructors indirectly through a public static method.

+16
source

From what I know, the general use of the private constructor is to ensure that the construction of the object is done using other means, i.e. a static method. For instance:

 public class Something { private Something() { } public static Something createSomething() { Something ret = new Something(); // configure ret in some specific way return ret; } } 

Thus, a private constructor restricts the instantiation of a class (and not how to extend it).

On the other hand, class designation as final used to explicitly say that a class cannot be extended. How it is created is a completely different matter.

So, although I'm not 100%, if there is a way to extend the class that has all the private constructors - the question is, why do you want to do this? Having a private constructor and marking a class as final have two different purposes.

+1
source

Source: https://habr.com/ru/post/987274/


All Articles