Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help with a interface concept

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with a interface concept

    Ok, I was reading some java when they drop this code on me.

    //As an example, here is a method for finding the largest object in a pair of objects, for any objects that are instantiated from a class that implements Relatable:

    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 can answer everything about this code, but only one little thing. Why is there a parentheses around Relatable in this Relatable obj1 = (Relatable)object1;?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,133
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with a interface concept

    Why is there a parentheses around Relatable
    That's called casting. The compiler wants you to tell it what type of object you are working with just to be sure you know what you are doing.

    There is also a check made at execution time that ensures that the object is of the type it is cast to. Otherwise you get an exception:
    Object obj = "asdf";
    Integer aI = (Integer)obj;

    java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
    Last edited by Norm; September 13th, 2011 at 02:18 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a interface concept

    Thanks your the best Norm. I hate to say this, but I will probably never come back to this site. Because, that was the only thing in java I could not figure out.
    Last edited by drakenlord; September 13th, 2011 at 02:31 PM.

Similar Threads

  1. THE MVVM AND MVC CONCEPT USING JAVASCRIPT,JQUERY,KNOCKOUTJS
    By gokulakrishnan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 9th, 2011, 11:54 PM
  2. Interface
    By kaasi in forum Member Introductions
    Replies: 4
    Last Post: April 21st, 2011, 10:45 AM
  3. Doubt in Encapsulation concept
    By nareshn in forum Object Oriented Programming
    Replies: 1
    Last Post: January 19th, 2011, 02:20 AM
  4. interface
    By nasi in forum Object Oriented Programming
    Replies: 5
    Last Post: September 2nd, 2010, 10:36 PM
  5. [SOLVED] The concept of Server and Client
    By rendy.dev in forum Java Theory & Questions
    Replies: 3
    Last Post: January 18th, 2010, 04:13 AM