Adam

Adam

  • NA
  • 131
  • 26.9k

Java interface

Sep 19 2011 12:10 PM
I am confused abou interfaces in java. If some class implements some interface, this class has to declar the bodies of methods inside interface. Lets say that is class A,B and C. If A implements intercafe and then I want use that method in B class which is implementig interface too, do I have to declare that method again in B class? or can I just use interface like a type of value in B class?

f.e: I have interface

public interface Relatable {     public int isLargerThan(Relatable other); }
class A implements Relatable{
public int isLargerThan(Relatable other) {     RectanglePlus otherRect = (RectanglePlus)other; // Why that way?     if (this.getArea() < otherRect.getArea())     return -1;     else if (this.getArea() > otherRect.getArea())     return 1;     else     return 0;     
}
class B implements Relatable{
public Object findLargest(Object object1, Object object2) {    Relatable obj1 = (Relatable)object1;    Relatable obj2 = (Relatable)object2;    if ( (obj1).isLargerThan(obj2) > 0) //Here Iam again using method but I dont declare it in that class, is it correct??       return object1;    else        return object2;         }
}

Source:
http://download.oracle.com/javase/tutorial/java/IandI/usinginterface.html



Answers (3)