Results 1 to 6 of 6
  1. #1
    Quentin's Avatar
    Quentin is offline Member
    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Default Interfaces and method existence assurance

    I've a question regarding the following snippet from Core Java. It explains why interfaces are needed in Java, but something other made me confused.

    Now you saw what a class must do to avail itself of the s... - justpaste.it (if I paste the quote here, I get the "Page not found" error after posting -.^)

    What made me confused are the underlined fragments. This is rather a question out of curiosity: the first fragment reads that the compiler must be sure that a method exits at a compile time, whereas the second fragment denies it - if a[i] doesn't have the specified compareTo method, a JVM simply throws an exception. Can someone clarify this issue?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Interfaces and method existence assurance

    In the first case, since a sort method should take a generic implementation of the Comparable interface, the compiler will ensure that the arrays are of the correct type. Otherwise, the compiler will generate an error, which is what you want it to do. This avoids runtime ClassCastExceptions.

    The second fragment was probably trying to explain the perils of not doing the above. The article criticized the designers for passing arrays to arrays of type Object instead of arrays of type Comparable, thus causing a ClassCastException if the array elements didn't implement the Comparable interface. On the surface, I might agree. But having read enough articles by the Array class authors and seeing that they were also accommodating a legacy merge sort, I suspect they knew what they were doing. Once a class is published is very difficult to change the public features of that class without breaking existing code that makes use of the class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Interfaces and method existence assurance

    The sort method predates Generics, so it can't accept anything other than an Object[].
    If they'd changed it to Comparable[] then they would likely have broken a shed load of code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Interfaces and method existence assurance

    I think the article was pointing out and questioning why they didn't just use the raw type of Comparable[] to begin with since both the Arrays class and the Comparable interface were included as of Java 1.2.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Interfaces and method existence assurance

    Ah, good point.
    And generics have nowt to do with it anyway...

    Is it Friday?
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Interfaces and method existence assurance

    Quote Originally Posted by Tolls View Post
    Is it Friday?
    :)
    Ah yes. But much too early to start drinking here in the states.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Check the existence of a parameter.
    By Jack_Tauson_Sr in forum New To Java
    Replies: 4
    Last Post: 03-21-2014, 01:11 AM
  2. Replies: 2
    Last Post: 02-05-2013, 04:35 PM
  3. Replies: 1
    Last Post: 08-02-2009, 12:52 PM
  4. Verifying existence of a table in a db
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 10:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •