Results 1 to 4 of 4
  1. #1
    sireesha is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Post i can't understand using interface as a type

    Hi friends,
    in the below code Relatable is the interface name.

    Java Code:
    public Object findLargest(Object object1, Object object2) {
       Relatable obj1 = (Relatable)object1;
       Relatable obj2 = (Relatable)object2;
       if ( (obj1).isLargerThan(obj2) > 0)
          return object1;
       else 
          return object2;
    }
    I read that we can use a reference type just like other datatypes.
    Then how can we call a function using reference datatype variable like in the above code ?
    and also they placed obj1 in the if condition in ().
    what it indicates ?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    they placed obj1 in the if condition in ().what it indicates
    Nothing really. Some people like to do it this way.
    Java Code:
    public class InterfaceRx {
        public static void main(String[] args) {
            Length length1 = new Length(100);
            Length length2 = new Length(50);
            Length larger = (Length)findLargest(length1, length2);
            System.out.println("larger = " + larger);
            Sequence seq1 = new Sequence(new int[]{5, 6, 7});
            Sequence seq2 = new Sequence(new int[]{3, 5, 7});
            Object o = findLargest(seq1, seq2);
            System.out.println("o = " + o);
            System.out.println("(Sequence)o  = " + (Sequence)o);
            System.out.println("(Relatable)o = " + (Relatable)o);
            Relatable r1 = new Length(12);
            Relatable r2 = new Length(32);
            Relatable r = (Relatable)findLargest(r1, r2);
            System.out.println("r = " + r);
            Relatable length = new Length(51);
            Relatable sequence = new Sequence(new int[]{1});
            //Object retVal = findLargest(length, sequence);
        }
    
        public static Object findLargest(Object object1, Object object2) {
            Relatable obj1 = (Relatable)object1;
            Relatable obj2 = (Relatable)object2;
            if (obj1.isLargerThan(obj2) > 0)
                return object1;
            else 
                return object2;
        }
    }
    
    class Sequence implements Relatable {
        int[] data;
        int length;
    
        Sequence(int[] data) {
            this.data = data;
            length = data.length;
        }
    
        public int isLargerThan(Object o) {
            Sequence aSequence = (Sequence)o;
            int len1 = length;
            int len2 = aSequence.length;
            int[] data2 = aSequence.data;
            if(len1 == len2) {
                for(int j = 0; j < length; j++) {
                    if(data[j] != data2[j])
                        return data[j] - data2[j];
                }
            }
            return len1 - len2;
        }
    
        public String toString() {
            String s = "[";
            for(int j = 0; j < length; j++) {
                s += data[j];
                if(j < length-1)
                    s += ", ";
            }
            s += "]";
            return "Sequence[data:" + s + ", length:" + length + "]";
        }
    }
    
    class Length implements Relatable {
        int value;
    
        Length(int value) {
            this.value = value;
        }
    
        public int isLargerThan(Object o) {
            int thisVal = this.value;
            int anotherVal = ((Length)o).value;
            return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
        }
    
        public String toString() {
            return "Length[value:" + value + "]";
        }
    }
    
    interface Relatable {
        public int isLargerThan(Object o);
    }

  3. #3
    sireesha is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    thank q very much,i have another doubt.
    obj1 is a variable of Relatable reference data type.
    Then how can we use this to call a method like
    obj1.isLargerThan(obj2)
    we can call a method only with class object.is this right ?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    Relatable r1 = new Length(12);
    Relatable r2 = new Length(32);
    System.out.println("r1.isLargerThan(r2) = " + r1.isLargerThan(r2));
    we can call a method only with class object.is this right
    Yes...
    Call instance methods with instance variable:
    Rectangle rect = new Rectangle(25, 25);
    int w = rect.getWidth();
    Call static (class) methods with ClassName:
    double distance = Point2D.distance(x1, y1, x2, y2);

Similar Threads

  1. help me need to understand queries
    By hossainsadd in forum JDBC
    Replies: 1
    Last Post: 05-26-2008, 01:02 AM
  2. Errors I don't understand
    By MattyB in forum New To Java
    Replies: 4
    Last Post: 04-02-2008, 12:55 AM
  3. Cannot understand whats wrong
    By Lehane_9 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 08:57 PM
  4. New: Want to understand Drawing...
    By diRisig in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 09:13 AM
  5. i don understand this error
    By Deon in forum New To Java
    Replies: 4
    Last Post: 01-12-2008, 11:03 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
  •