Results 1 to 4 of 4
- 11-20-2007, 07:16 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 50
- Rep Power
- 0
i can't understand using interface as a type
Hi friends,
in the below code Relatable is the interface name.
I read that we can use a reference type just like other datatypes.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; }
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 ?
- 11-20-2007, 09:22 PM #2
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); }
- 11-20-2007, 09:28 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 50
- Rep Power
- 0
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 ?
- 11-20-2007, 10:07 PM #4
we can call a method only with class object.is this rightJava Code:Relatable r1 = new Length(12); Relatable r2 = new Length(32); System.out.println("r1.isLargerThan(r2) = " + r1.isLargerThan(r2));
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
-
help me need to understand queries
By hossainsadd in forum JDBCReplies: 1Last Post: 05-26-2008, 12:02 AM -
Errors I don't understand
By MattyB in forum New To JavaReplies: 4Last Post: 04-01-2008, 11:55 PM -
Cannot understand whats wrong
By Lehane_9 in forum New To JavaReplies: 1Last Post: 03-06-2008, 07:57 PM -
New: Want to understand Drawing...
By diRisig in forum New To JavaReplies: 1Last Post: 02-05-2008, 08:13 AM -
i don understand this error
By Deon in forum New To JavaReplies: 4Last Post: 01-12-2008, 10:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks