Hallo! I have a question about the "public final class Math".
I am not able to inherit this class, because it is final? understandable, we need its methods for calculating.
This class is not abstract and not an interface, it extends Object directly.
There are not any constructors with parameters.
But codegym teached me in one of its articles, that every class in Java has a default constructor.
So why I am not able to create a Math object?
What didn't I understand?
Question about class math
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Richard
30 August, 22:04
thanks for replying.
0
Guadalupe Gagnon
28 August, 15:14
A default constructor is a constructor with no parameters that is added ONLY when no other constructors are present. Whenever ANY constructor is present in the specified class then no default constructor will automatically be added.
In the Math class they have a constructor (or many, I didn't look at all the code) and made it private, which means it does not have a default constructor and no class outside of the Math class can access that constructor. This has the effect that you described in which case you can not call the constructor and create an object of that class. This doesn't matter as you don't need an object to use static methods and all the public methods of the Math class are static, so there is no reason to create an object of that class.
A final class can not be extended; which you theorized correctly.
You can't extend the class but, if you are really dead set on creating your own implementation of the Math class, you can create a class and copy the methods that you need from Math and have them return the Math class's methods return values, then add the additional functionality that you want (which is the reason to extend a class). This would be similar to wrapping a class. See example below:
0