• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date.format() single-argument parameter anomaly

 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In using eclipse indigo, with the compiler set to 1.6, I was crashing at runtime on the following:

I got an IllegalArgumentException, "Cannot format given Object as a Date".

I scratched my head for a while, then finally realized that I was passing a Calendar object to SimpleDateFormat.format(), which takes a Date object.

The odd thing is that it wasn't caught at compile time. Calendar doesn't extend Date. The documentation I find online for SimpleDateFormat and DateFormat in version 1.4 and version 6 say that the format method takes a Date object. The source code for version 6 has 'public final String format(Date date)', and then it calls the method that likely throws this error. When I changed the call to be , things worked as expected.

Why didn't the compiler catch this?

rc
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because DateFormat extends Format, and that defines method public final String format(Object obj).
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But DateFormat defines format() as taking a Date -- I would expect the compiler to use the definition of DateFormat.format(), not the method that it overloads from the Format class. The DateFormat definition uses Date.

Or is that overloading? If class B extends class A, and implements a single-parameter method from A, and defines that single parameter as a subclass of the parameter from A, does the compiler not check the parameter for the specific subclass as defined by B?

Both the methods in this case are final, I don't know if that makes any difference.

To restate by example, given the following:

The compiler does not catch the fact (in the case of DateFormat and Format) that the parameter to m1 is incorrect. Why not?

rc
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it's overloading. Assuming B was supposed to extend A in that example, then class B has two overloaded m1 methods, and Calendar will match to one of them. As it stands you should get an exception, though.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ralph Cook wrote:I would expect the compiler to use the definition of DateFormat.format(), not the method that it overloads from the Format class.


Why ? DateFormat.format expects a Date object and you were passing it a Calendar object. Therefore it can't use that method so it works it way up the class hierarchy and finds Format.format(Object obj) which it can use.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you overload a method in a sub class doesn't mean that the original method "disappears". DateFormat and SimpleDateFormat still have the format(Object) method, and because their own format(Date) method doesn't match they will use the other one instead, as Joanne said.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course - thank you Ms. Neal and Mr. Spoor. I suppose the explanation was implied in the original answer, but I still didn't understand it.

Yes, B is supposed to extend A in my quickie example.

rc
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic