Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct a Non Instantiable AND Non Inheritable Class in Java

Tags:

java

The question says it all. I know the Singleton pattern (with final to its class) is a solution. Are there any other possible ways we can achieve this? Abstracting a class makes it non-instantiable. Making it final makes it non-inheritable. How do we combine both?

public final class SingletonObject
{
  private SingletonObject()
  {
    // no code req'd
  }

  /*public static SingletonObject getSingletonObject()
  {
    if (ref == null)
        // it's ok, we can call this constructor
        ref = new SingletonObject();        
    return ref;
  }*/

  public Object clone()
    throws CloneNotSupportedException
  {
    throw new CloneNotSupportedException(); 
    // that'll teach 'em
  }

  private static SingletonObject ref;
}

Code Ref: http://www.javacoffeebreak.com/articles/designpatterns/index.html

like image 673
Ashok Goli Avatar asked Jan 13 '12 09:01

Ashok Goli


People also ask

How do we make classes which are not Instantiable not inheritable?

We can make a class non-instantiable by making the constructor of the class private . By making the constructor private, the following happens: The constructor becomes inaccessible outside the class, so class objects can't be created.

How do you make a class non inheritable in Java?

There are 2 ways to stop or prevent inheritance in Java programming. By using final keyword with a class or by using a private constructor in a class.

What is non Instantiable?

Not instantiable; that cannot be instantiated.

How should a class be declared to make that class as non inheritable?

you can use final keyword to class can not be inherited.


2 Answers

Make the constructor private:

public final class Useless {
    private Useless() {}
}

A private constructor is the normal object-oriented solution. However, it would still be possible to instantiate such a class using reflection, like this:

Constructor<Useless> con = Useless.class.getDeclaredConstructor();
con.setAccessible(true); // bypass "private"
Useless object = con.newInstance();

To prevent even reflection from working, throw an exception from the constructor:

public final class Useless {
    private Useless() {
        throw new UnsupportedOperationException();
    }
}
like image 145
Bohemian Avatar answered Sep 20 '22 18:09

Bohemian


You mean a class with static methods only? Class cannot be both final and abstract. But you can use private constructor to make it not instantinable.

final class StaticOnly {

    private StaticOnly() {
        throw new RuntimeException("Do not try to instantiate this");
    }

    public static String getSomething() {
       return "something";
    }
}

Below example will work to. You won't instantiate it because it's abstract. You won't inherit it because there is no way to call super constructor from external subclass (only inner subclass will work)

abstract class StaticOnly {

    private StaticOnly() {}

    public static String getSomething() {
         return "something";
    }
}

enum will work too

enum StaticOnly {
    S;

    public static String getSomething() {
        return "something";
    }
}

but it always have at least one instance (here it's S).

like image 30
Piotr Gwiazda Avatar answered Sep 22 '22 18:09

Piotr Gwiazda