Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java-Final class vs private constructor: Difference between java.util.Arrays and java.lang.Math

I know the difference between a final class (you can't inherit the class) and a private constructor (you can't create an instance of the class). But why Arrays and Math, classes of Java, have both private constructors but Math is final class and Arrays is not?

What is the difference? Are not both utility classes?

Thanks

like image 267
Antonio1996 Avatar asked Jan 27 '17 12:01

Antonio1996


People also ask

What is the difference between private class and final class in Java?

The only difference between private and final methods is that in case of final methods we even can't define a method with the same name in child class while in case of private methods we could define.

What is the difference between private and private final in Java?

The main difference between private and final keywords in Java is that private is primarily an access modifier, which controls the visibility of variables, methods, and classes in Java applications, while final is just a modifier that enforces additional constraints on the field, method, and class in Java.

Does final class have constructor in Java?

No, a constructor can't be made final. A final method cannot be overridden by any subclasses. As mentioned previously, the final modifier prevents a method from being modified in a subclass. The main intention of making a method final would be that the content of the method should not be changed by any outsider.

Does a final class need a constructor?

declaring constructor as final In inheritance whenever you extend a class. The child class inherits all the members of the superclass except the constructors. In other words, constructors cannot be inherited in Java, therefore, you cannot override constructors. So, writing final before constructors make no sense.


1 Answers

When class has a private constructor but is not final, you can define inner classes in the same class file that have public constructors and can be instantiated. But you cannot define any subclasses outside of that initial class file. For example this would compile:

public class Animal {
  public void say() {
    System.out.printLn("Animal says:");
  }
  private Animal() {}

  public static class Cat extends Animal {
    public Cat() {super();}
    @Override public void say() {
      super.say();
      System.out.printLn("Meow");
    }
  }
}

However, this (defined in another file) will not:

public class Dog extends Animal {
  public Dog() {super();} // compilation error here: The constructor Animal() is not visible
  @Override public void say() {
    super.say();
    System.out.printLn("Wuf");
  }
}

This technique allows you to define class hierarchy where you have full control over what types can extend your class (because all these subtypes have to be enumerated inside of same class file).

That said, java.util.Arrays is not defined as non-final because of the above - it is probably just an implementor's oversight that does not matter all that match, so it was not fixed to date.

like image 55
M. Prokhorov Avatar answered Sep 28 '22 10:09

M. Prokhorov