How do I prevent the default constructor from being used in Java?
In my assessment it says:
"We don't want the user to use the default constructor since the user has to specify the HashCode, and maximum load factor"
I thought this would do the trick, but apparently not (dictionary is a class that is used to throw exceptions):
public boolean HashDictionary() throws DictionaryException {}
DictionaryException Class:
public class DictionaryException extends Throwable {
}
Test to make sure it throws an exception when using default contructor(Supplied by lecturer):
try
{
HashDictionary h = new HashDictionary();
System.out.println("***Test 1 failed");
}
catch (DictionaryException e) {
System.out.println(" Test 1 succeeded");
}
I just want to know how I could do this, as I'm not familiar with a method of doing it. Thanks.
You can declare the default one as private if you do not want it to be called.
To answer your comment, you can throw an exception-
public HashDictionary() throws DictionaryException {
throw new DictionaryException("Default constructor is not allowed.");
}