Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-20-2007, 08:16 PM
Member
 
Join Date: Nov 2007
Posts: 45
sireesha is on a distinguished road
i can't understand using interface as a type
Hi friends,
in the below code Relatable is the interface name.

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 ?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-20-2007, 10:22 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
they placed obj1 in the if condition in ().what it indicates
Nothing really. Some people like to do it this way.
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); }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-20-2007, 10:28 PM
Member
 
Join Date: Nov 2007
Posts: 45
sireesha is on a distinguished road
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 ?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-20-2007, 11:07 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
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);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help me need to understand queries hossainsadd Database 1 05-26-2008 01:02 AM
Errors I don't understand MattyB New To Java 4 04-02-2008 12:55 AM
Cannot understand whats wrong Lehane_9 New To Java 1 03-06-2008 08:57 PM
New: Want to understand Drawing... diRisig New To Java 1 02-05-2008 09:13 AM
i don understand this error Deon New To Java 4 01-12-2008 11:03 AM


All times are GMT +3. The time now is 06:39 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org