Results 1 to 5 of 5
Like Tree2Likes
  • 2 Post By Fubarable

Thread: Interface

  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Interface

    I want to restrict a static method, so it only accept an object if it have implements a specific interface. Say, I want a method that only accept an object if its comparable.
    Is this the professional way to code it?

    Java Code:
    static void doNothing (Object obj)
    {
    	if (!(obj instanceof Comparable))
    		throw new Exception ();
    
    	//If this object do implement Comparable
    	Comparable comp = (Comparable) obj;
    	//Now can we use compareTo method.
    }
    Shall I use this, or type parameter?
    Also, can someone explain to me how java.util.Arrays.sort work(not the sorting algorithm, but rather how it can use compareTo)? I checked the source, but it keep sending me to other methods and classes.

    Also, is this how serial methods check whether or not a class is serializable?
    Java Code:
    void someSerialMethod (Object obj)
    {
    	if (!(obj instanceof java.io.Serializable))
    		//Stop the operation
    }
    Last edited by Pojahn_M; 09-18-2011 at 12:33 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Interface

    Quote Originally Posted by Pojahn_M View Post
    I want to restrict a static method, so it only accept an object if it have implements a specific interface. Say, I want a method that only accept an object if its comparable.
    Is this the professional way to code it?

    Java Code:
    static void doNothing (Object obj)
    {
    	if (!(obj instanceof Comparable))
    		throw new Exception ();
    
    	//If this object do implement Comparable
    	Comparable comp = (Comparable) obj;
    	//Now can we use compareTo method.
    }
    No, you would use that type as the parameter type:

    Java Code:
    static void doNothing (Comparable comp) {
    	//Now can we use compareTo method.
    }
    The advantage being now the compiler will check that you're using the correct argument at compile time rather than exception being thrown at run time.

    Also, can someone explain to me how java.util.Arrays.sort work(not the sorting algorithm, but rather how it can use compareTo)? I checked the source, but it keep sending me to other methods and classes.
    I don't know "how it works" but all I know is that it does work if I create a decent compareTo method. The method is very basic -- if the current object's "value" (and how to calculate that is something you have to decide) is greater than the parameter object, the method returns 1, if less, it returns -1, and if both object's "value" are the same, then it returns 0.
    Tolls and DeNiS_M like this.

  3. #3
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: Interface

    I found out how sort methods works.
    Sure, your way is better, I dunno why Oracle coded it like this:
    Oracle code:
    Java Code:
        private static void mergeSort(Object[] src,
                                      Object[] dest,
                                      int low,
                                      int high,
                                      int off) {
            int length = high - low;
    
            // Insertion sort on smallest arrays
            if (length < INSERTIONSORT_THRESHOLD) {
                for (int i=low; i<high; i++)
                    for (int j=i; j>low &&
                             [B]((Comparable) dest[j-1]).compareTo(dest[j])>0[/B]; j--)
                        swap(dest, j, j-1);
                return;
            }
    What are the benefits of using Object as parameter, instead of an interface?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Interface

    Quote Originally Posted by Pojahn_M View Post
    What are the benefits of using Object as parameter, instead of an interface?
    Backward compatibility. Their code has to work with the newer generic code from version 1.5 and above, and the non-generic code from previous Java versions.

  5. #5
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: Interface

    final questiton, what is the different between this:

    Java Code:
    public static void compare (Comparable comp1, Comparable comp2)
    {
    	...
    	comp1.compareTo (comp2);
    	//More code
    }
    and this:

    Java Code:
    public static <T extends Comparable<? super T>> void compare (T comp1, T comp2)
    //Can also be written public static <T extends Object & Comparable<? super T>> void compare (T comp1, T comp2)
    {
    	...
    	comp1.compareTo (comp2);
    	//More code
    }

Similar Threads

  1. What is interface?
    By Sayan_P007 in forum New To Java
    Replies: 11
    Last Post: 06-22-2011, 01:14 PM
  2. Interface?
    By makpandian in forum New To Java
    Replies: 5
    Last Post: 03-26-2009, 10:59 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
  •