Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java -- private constructor vs final and more

Suppose there is a class with all of its constructors declared as private.

Eg.:

public class This {
    private This () { }

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

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

However, the Eclipse messages i'm getting are giving me the impression that this is possible-- an all-constructors-private class can be extended. Take a look at this:

When I attempt to extend this class with something like

public class That extends This {
    ...
}

Eclipse giving me an error that: "Implicit super constructor This() is not visible for default constructor. Must define an explicit constructor."

When i define a constructor of its own:

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

this time i'm getting : "Implicit super constructor This() is not visible for default constructor. Must explicitly invoke another constructor."

Is there a way to get around this-- of extending a class of which all constructors are private?

if yes, how?

if no, what's the difference between stopping a class from being extended by i.) making its constructors private, and ii.) defining it as final?

Note: i saw Can a constructor in Java be private? among some other discussions.

like image 315
Roam Avatar asked Aug 30 '13 00:08

Roam


People also ask

Should final class have private constructor?

Using Private Constructors to Prevent Subclassing If we tried to create such as subclass, it would be unable to call the super constructor. However, it's important to note that we'd normally make a class final to prevent subclassing rather than using a private constructor.

Should Java constructors be private?

As an example of delegating constructors, the following class allows you to save a value and a type, but it only lets you do it for a subset of types, so making the general constructor private is needed to ensure that only the permitted types are used. The common private constructor helps code reuse.

What is the benefit of private constructor?

Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static.

When would you use a private constructor in Java?

A private constructor in Java is used in restricting object creation. It is a special instance constructor used in static member-only classes. If a constructor is declared as private, then its objects are only accessible from within the declared class. You cannot access its objects from outside the constructor class.


1 Answers

You declare a class final vs. make its constructor private for different reasons:

  • You make class final to indicate that the class is not designed for inheritance.
  • You make all constructors private to give the class the control over its instantiation.

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

Note that declaring constructors private disables inheritance only from the outside. Inside the class, you may still inherit it with a named or an anonymous derived class.

When you make all constructors of the class private you need a static method that is public to make the class usable. One common kind of the static method is factory method: you can let the users of your class call private constructors indirectly through a public static method.

like image 105
Sergey Kalinichenko Avatar answered Oct 05 '22 03:10

Sergey Kalinichenko