Results 1 to 4 of 4
  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Interface as a type

    Since interface can be used to create a reference variable with the interface for its type, if and only if the reference variable refers to object which implements the interface.

    Java Code:
    [B]package[/B] interfaceRelatable;
     
    [B]public[/B] [B]interface[/B] Relatable {
        
        // this (object calling isLargerThan)
        // and other must be instances of 
        // the same class returns 1, 0, -1 
        // if this is greater // than, equal 
        // to, or less than other
        [B]public[/B] [B]int[/B] isLargerThan(Relatable other);
    }
     
     
    [B]public[/B] [B]class[/B] Point {
           [B]public[/B] [B]int[/B] x;
           [B]public[/B] [B]int[/B] y;
           Point([B]int[/B] x, [B]int[/B] y){
                  [B]this[/B].x=x;
                  [B]this[/B].y=y;
           }
    }
     
     
     
    [B]public[/B] [B]class[/B] RectanglePlus 
    [B]implements[/B] Relatable {
    [B]public[/B] [B]int[/B] width = 0;
    [B]public[/B] [B]int[/B] height = 0;
    [B]public[/B] Point origin;
     
    // four constructors
    [B]public[/B] RectanglePlus() {
        origin = [B]new[/B] Point(0, 0);
    }
    [B]public[/B] RectanglePlus(Point p) {
        origin = p;
    }
    [B]public[/B] RectanglePlus([B]int[/B] w, [B]int[/B] h) {
        origin = [B]new[/B] Point(0, 0);
        width = w;
        height = h;
    }
    [B]public[/B] RectanglePlus(Point p, [B]int[/B] w, [B]int[/B] h) {
        origin = p;
        width = w;
        height = h;
    }
     
    // a method for moving the rectangle
    [B]public[/B] [B]void[/B] move([B]int[/B] x, [B]int[/B] y) {
        origin.x = x;
        origin.y = y;
    }
     
    // a method for computing
    // the area of the rectangle
    [B]public[/B] [B]int[/B] getArea() {
        [B]return[/B] width * height;
    }
     
    // a method required to implement
    // the [U]Relatable[/U] interface
               
    [B]public[/B] [B]int[/B] isLargerThan(Relatable other) {
        RectanglePlus otherRect 
            = other;                                               //**************ERROR*********** If "other" type casted to RectanglePlus, error goes away.
        [B]if[/B] ([B]this[/B].getArea() < otherRect.getArea())
            [B]return[/B] -1;
        [B]else[/B] [B]if[/B] ([B]this[/B].getArea() > otherRect.getArea())
            [B]return[/B] 1;
        [B]else[/B]
            [B]return[/B] 0;   
    }
    }
      
     
    [B]public[/B] [B]class[/B] Main {
           [B]public[/B] [B]static[/B] [B]void[/B] main(String[] args) {
                  RectanglePlus r1= [B]new[/B] RectanglePlus(20,30);
                  Point p= [B]new[/B] Point (0,0);
                  RectanglePlus r2= [B]new[/B] RectanglePlus(p, 23, 25);
                 
                  [B]if[/B] (r1.isLargerThan(r2)==-1){
                         System.[I]out[/I].println("r1 is smaller than r2.");   
                  }
                  [B]else[/B] [B]if[/B] (r1.isLargerThan(r2)==1){
                         System.[I]out[/I].println("r1 is greater than r2.");   
                  }
                  [B]else[/B] [B]if[/B] (r1.isLargerThan(r2)==0){
                         System.[I]out[/I].println("r1 is equal to r2.");       
                  }
           }
     
    }

    As mentioned in the erroneous line of code, if "other" is casted to RectanglePlus, the error goes away. But my problem is not the error. My problem is WHY ????
    Since other is created as a reference variable using Relatable as its type (Relatable other;), it means other is supposed to contain reference of object of class RectanglePlus (since this is the class implementing Relatable). In the next line which actually gives the error, other is assigned to a reference variable otherRect of type RectanglePlus which is also a reference variable supposed to refer to RectangleType objects.
    Don't forget to smile :-)

  2. #2
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Interface as a type

    I am sorry about my code, I don't even know what has happened to it after I have submitted the post. I did not use any formatting inside the code. I just copied it from eclipse.
    Don't forget to smile :-)

  3. #3
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,787
    Rep Power
    7

    Default Re: Interface as a type

    Java Code:
    public int isLargerThan(Relatable other) {
        RectanglePlus otherRect = other;
    Imagine you have another class that implements Relatable, eg Foo. Now what happens if a Foo object is passed as a parameter to the isLargerThan method? Foo is not a RectanglePlus object and therefore that line of code goes KABOOM!

  4. #4
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Interface as a type

    Thank you.
    Don't forget to smile :-)

Similar Threads

  1. Unknown type of an interface field
    By vallach in forum New To Java
    Replies: 2
    Last Post: 11-27-2012, 09:49 AM
  2. uses interface as type
    By j2me64 in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2011, 02:31 PM
  3. Interface return type invalid
    By Karenthian in forum New To Java
    Replies: 8
    Last Post: 05-09-2011, 11:58 PM
  4. Interface as a type!
    By guro in forum New To Java
    Replies: 4
    Last Post: 03-10-2009, 07:03 AM
  5. i can't understand using interface as a type
    By sireesha in forum New To Java
    Replies: 3
    Last Post: 11-20-2007, 10:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •