• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Math Class

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
While going to JLS I found out that the java.lang.Math class is having a constructor that is a private one thats the reason we r unable to create a object of Math class.
Can anyone please clarify what is the reason for declaring the constructor as a private one when there is no function available with the help of which i can create an object.Why can't the class be an abstract class.Whats the advantage of declaring the constructor as a private against declaring the class as an abstract.
Thanx in Advance
Sada
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Math class isn't abstract because it contains a bunch of static methods. An abstract class would define only abstract methods, but they would all have to be overridden in subclasses or classes that implement the abstract class. Math's static methods contain code, and so the class its self can't be abstract.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The developers of the Math class did not want you to ever create an instance of Math. When you think about it - what is a MATH??? Therefore they made the constructor private. What they do want you to do is to use the methods of the math class without making an instance. That is why all of the methods are static (don't need an instance to use the methods)
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Nick,
sorry, but that's not right...

Originally posted by Nick Riviera:
An abstract class would define only abstract methods, but they would all have to be overridden in subclasses or classes that implement the abstract class. Math's static methods contain code, and so the class its self can't be abstract.


First: the abstract keywords tells you, that you can't instantiate this class. That's all. You can have methods which are NOT abstract, and you won't get a compile error.
Second: Of course you can have static methods in an abstract class. No compiler will prevent you to do that...
See the following code for clarification

Hope that clarifies the concept of abstract classes
cheers
Oliver
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, but the original question was "Why use private constructors when you could declare the class abstract"


The answer is:

  1. Sun (quite rightly) don't want us idiots subclassing Math and changing the functionality. That's why the Math class is declared final.
  2. You can't declare an abstract class final. Therefore, if Math were implemented as an abstract class, it could be subclassed.

  3. So... it's not just a random choice between the two methods. By using the private constructors technique you can stop people both instantiating and subclassing Math. Yippe kay aye!
 
Sadashiv Borkar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave,
Thanx for the answer it was really a nice one which cleared my doubts.
bye
take care
sada
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost 12 years since Dave's answer and I found it really useful. Thank you Dave.
 
Marshal
Posts: 79716
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

That is probably a record, a 12‑year‑old thread reopened. There is a Java Language Specification section about uninstantiable classes here.
 
This will take every ounce of my mental strength! All for a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic