*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Math Class is Final? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Math Class is Final?" Watch "Math Class is Final?" New topic
Author

Math Class is Final?

Sarang Gurao
Greenhorn

Joined: Mar 14, 2001
Posts: 13
Hello Friends
I had some problem regarding the private constructor and having the final class. according to Robert Heller the Math class is final (so that it cannot be subclassed) and the constructor is private(so that it cannot have instance).
class A
{
private A(){}
}
class B extends A
{
}
According to RHE Math class is final class and also had a private constuctor.
Having only a private constructor and not having the class final could also make the in inability to make subclass and to have the instance of that class. then why Math class is made final.
Rakesh Patel
Greenhorn

Joined: Jan 18, 2001
Posts: 19
According to JavaDoc API, API doesn't mention anything about Math class' constructor so check it out.
lee dalais
Ranch Hand

Joined: Feb 28, 2001
Posts: 67
hi Rakesh
if u look at the Math.java source file, it declares a private constructor, even though javadoc doesn't mention it.
Here's a question, why would u want to subclass or instantiate the Math class? what would u want to initialize by calling the constructor if it wasn't private?
with regards to why Math is final, so u cannot override it.
as far as i know a final method cannot be overridden so a final class cannot also be overridden.
pls correct where i am wrong!
Bob Lancaster
Greenhorn

Joined: Feb 21, 2001
Posts: 4
The Math class is Java's answer to the old Math.h methods from C/C++. It is way to introduce a bunch of mathematical functions that don't really belong to any object. In order to have these methods while still adhering to Java's rule that everything must be in a class, the methods are all declared static and final and are put in a final class which, due to the private constructor, can never be instantiated. Since it is final, Math can't be subclassed. However, what would be a subclass of Math?
It's really a violation of strict OO programming, but the methods are useful, so its OK.
-Bob Lancaster
SCJP, working on SCJD

Sarang Gurao
Greenhorn

Joined: Mar 14, 2001
Posts: 13
Thank For kind response but i am still confuse that having a private constructor can only satisfy the condition that the class having private default constructor cannot be subclass as well as the class cannot have instance in subclass then why Math class is made final plus the constructor is also made private.
only the constructor made private and class not defined final can
also do the thing.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Having a private constructor does not prevent you from making an instance of a class. It only forces you to use a method of the class to create the instance instead of the constructor. The method (which CAN get at the private constructor) then calls the correct constructor and creates it for you.
Of course SUN DIDN'T provide a createMath() method (smart guys ).
If they JUST make the constructor private, we could sub-class Math and add silly math methods to it, and override their good methods with our not so good methods. Not a good idea.
If they JUST make the class FINAL, then we could create an instance of MATH, which doesn't make sense either.
So they did both.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
wood burning stoves
 
subject: Math Class is Final?