Question 1: I'm still very very new to Java and in the course of my self-study I've started to look at interfaces. I think I understand the concept of an interface, but I'm having trouble determining why someone would use it. I'm going through the Sam's "learn Java in 21 days" book and using the resourses available on Sun's site explaining interfaces to try to figure this out, but I'm still lost. Below is a sample program from Sun's learning Java tutorial located on their website.
Original Link:
http://java.sun.com/docs/books/tutorial/java/IandI/usinginterface.html
First, an interface class file has been created:
public interface Relatable {
public int isLargerThan(Relatable other);
}
Then a class is created to implement the interface:
import java.awt.Point;
public class RectanglePlus implements Relatable {
public int width = 0;
public int height = 0;
public Point origin;
// four constructors
public RectanglePlus() {
origin = new Point(0, 0);
}
public RectanglePlus(Point p) {
origin = p;
}
public RectanglePlus(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
public RectanglePlus(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x;
origin.y = y;
}
// a method for computing the area of the rectangle
public int getArea() {
return width * height;
}
// a method to implement Relatable
public int isLargerThan(Relatable other) {
RectanglePlus otherRect = (RectanglePlus)other;
if (this.getArea() < otherRect.getArea())
return -1;
else if (this.getArea() > otherRect.getArea())
return 1;
else
return 0;
}
}
From here, I created my own class file to test (and try to understand) the concept:
class RectComp {
public static void main(String[] Args) {
int rect1 = 5;
int rect2 = 4;
RectanglePlus test = new RectanglePlus(rect1, rect1);
RectanglePlus test2 = new RectanglePlus(rect2, rect2);
System.out.println(test.isLargerThan(test2));
}
}
This program works as expected and for the values provided, returns 1. The problem I run into is when I make the following changes to the 'RectanglePlus' file:
import java.awt.Point;
// This is a demonstration of the concept of implementation
// public class RectanglePlus implements Relatable {
// Test change to determine if this can be done w/o an interface
public class RectanglePlus {
// Class variables
public int width = 0;
public int height = 0;
public Point orgin;
// Four constructor methods, based on agruments provided
public RectanglePlus() {
orgin = new Point(0, 0);
}
public RectanglePlus(Point p) {
orgin = p;
}
public RectanglePlus(int w, int h) {
orgin = new Point(0, 0);
width = w;
height = h;
}
public RectanglePlus(Point p, int w, int h) {
orgin = p;
width = w;
height = h;
}
// A class method used to move the orgin point
public void move(int x, int y) {
orgin.x = x;
orgin.y = y;
}
// A class method used to determine the area of a rectangle
public int getArea() {
return width * height;
}
// a method to implement Relatable (?)
public int isLargerThan(RectanglePlus other) {
RectanglePlus otherRect = (RectanglePlus)other;
if (this.getArea() < otherRect.getArea())
return -1;
else if (this.getArea() > otherRect.getArea())
return 1;
else
return 0;
}
}
When using this file, the program works the same exact way and doesn't implement anything. I don't see the purpose of the interface. Can someone help me understand this concept better by explaining to me why I would want to implement an interface as opposed to simply writing the class itself as an argument?
Question 2: Using the same program above, specifically one line:
RectanglePlus otherRect = (RectanglePlus)other;
I don't know what this means, I don't know how it works. My book used it too (in a slightly different context, using
int z =(int)(x/y);
I understand it's declaring a variable z, but I don't know what z ends up being.
I appriciate any and all help someone can provide me with these problems.