Interface and type casting - java

In the Java 8 tutorial about interface, one example says that when a class implements an interface, one has to type cast the interface type into the class type in order to invoke methods of this class, as shown by the following example from the java 8 tutorial:
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 required to implement
// the Relatable interface
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;
}
}
In the method isLargerThan(Relatable other), other is casted to type RectanglePlus in order to invoke getArea().
In the other example about default methods in interface, the compareTo(Card o) method doesn't type cast o to type PlayingCard, but can invoke int hashCode() directly, I don't understand this. Thanks for your help.
package defaultmethods;
public class PlayingCard implements Card {
private Card.Rank rank;
private Card.Suit suit;
public PlayingCard(Card.Rank rank, Card.Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Card.Suit getSuit() {
return suit;
}
public Card.Rank getRank() {
return rank;
}
public boolean equals(Object obj) {
if (obj instanceof Card) {
if (((Card)obj).getRank() == this.rank &&
((Card)obj).getSuit() == this.suit) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public int hashCode() {
return ((suit.value()-1)*13)+rank.value();
}
public int compareTo(Card o) {
return this.hashCode() - o.hashCode();
}
public String toString() {
return this.rank.text() + " of " + this.suit.text();
}
public static void main(String... args) {
new PlayingCard(Rank.ACE, Suit.DIAMONDS);
new PlayingCard(Rank.KING, Suit.SPADES);
}
}

In short: Because hashCode is defined in java.lang.Object and every other class extends Object implicitly.
So when you have
public int compareTo(Card o) {
return this.hashCode() - o.hashCode();
}
the compiler already knows that o is of type Card which extends Object which defines a hashCode method. No need for an explicit cast.
On the other hand in your isLargerThan method the parameter is of type 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;
}
And judging from the link you provided, the getArea method is defined in RectanglePlus only. Since the compiler only sees Relatable it does not know anything about a getArea method at this point and you need to explicitly cast other to RectanglePlus to be able to access it.
Note that you should actually do an instanceof check before casting to avoid a ClassCastException when other is not a RectanglePlus (you don't know if there might be other classes implementing Relatable).
Let me try a non-code related example:
If people have a pet they usually give it a name. So whatever pet you have, one can always ask for its name (cf. hashCode). But they cannot ask you to make it bark (cf. getArea) unless they know that it is a dog.
And you will probably fail to make a cat bark (cf. ClassCastException).

Related

Why is this set containment check failing?

I am going through the Effective Java 3rd edition and I was reading Item 10: Follow Equals contract when overriding.
There is an example there which I was trying to simulate on my machine. Below is the code for the same.
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
#Override
public boolean equals(Object obj) {
if (!(obj instanceof Point))
return false;
Point p = (Point)obj;
return (x == p.x) && (y ==p.y);
}
// Use this for demonstration with AtomicPoint class.
/*#Override
public boolean equals(Object obj) {
if(obj == null || (obj.getClass() != getClass())) {
return false;
}
Point p = (Point)obj;
return p.x == x && p.y == y;
}*/
}
public class AtomicPoint extends Point{
private static final AtomicInteger counter = new AtomicInteger();
public AtomicPoint(int x, int y) {
super(x, y);
counter.incrementAndGet();
}
public static int getCounter() {
return counter.get();
}
private static Set<Point> sampleSet = new HashSet<>();
public static void main(String[] args) {
sampleSet.add(new Point(1,2));
sampleSet.add(new Point(1,3));
sampleSet.add(new Point(1,4));
AtomicPoint ap = new AtomicPoint(1,3);
// This should return true but its returning false
System.out.println(sampleSet.contains(ap));
}
}
As you can see from the comment in the AtomicPoint class, I am getting false for the contains check, whereas Joshua Bloch states that this should return true. Can someone help me here?
For using HashSet<T> or HashMap<T> you need to override hashCode() methods from super class. You should have in your editor automatically generating hashCode() and equals() methods (and i'm suggesting you to use that always in every class). If you want to use TreeSet<T> or TreeMap<T> you will need to implement Comparable or Comparator<T> interface and override their compare() or compareTo() methods for using it.

My class does not override abstract method compareTo

My class details attributes of restaurants downtown, said attributes being x/y locations and rank. The problem is, whenever I run the program It throws an error, saying that non-abstract class "Downtown" does not override abstract method "compareTo". I cannot make this class abstract because I need to initialise the object outside this block of code. Where does my program go wrong? Is there a problem with my compareTo implementation? Any suggestions will be much appreciated.
public class Downtown implements Comparable<Downtown> {//Throws error on this line
private int x;
private int y;
private int rank;
public Downtown(int x, int y, int rank) {
this.x = x;
this.y = y;
this.rank = rank;
}
//Appropriate setters and getters for x , y and rank
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public int compareTo(Downtown p1, Downtown p2)//Actual comparison
{
// This is so that the sort goes x first, y second and rank last
// First by x- stop if this gives a result.
int xResult = Integer.compare(p1.getX(),p1.getX());
if (xResult != 0)
{
return xResult;
}
// Next by y
int yResult = Integer.compare(p1.getY(),p2.getY());
if (yResult != 0)
{
return yResult;
}
// Finally by rank
return Integer.compare(p1.getRank(),p2.getRank());
}
#Override
public String toString() {
return "["+x+' '+y+' '+rank+' '+"]";
}
Java's Comparable<T> interface defines compareTo method as follows:
int compareTo(T o);
This means that the method must take one parameter; the other parameter is the object itself, i.e. this. You need to implement this one-argument method in place of your two-argument method to fix this problem.
Compiler will help you figure out issues like this by using #Override annotation on your method:
#Override // Issues an error
public int compareTo(Downtown p1, Downtown p2)
#Override // Compiles fine
public int compareTo(Downtown other)
The compareTo method should compare the current object (this) to just one other. It shouldn't have two parameters for comparison. You could write your method like this.
public int compareTo(Downtown p2)//Actual comparison
{
// This is so that the sort goes x first, y second and rank last
// First by x- stop if this gives a result.
int xResult = Integer.compare(getX(),p2.getX());
if (xResult != 0)
{
return xResult;
}
// Next by y
int yResult = Integer.compare(getY(),p2.getY());
if (yResult != 0)
{
return yResult;
}
// Finally by rank
return Integer.compare(getRank(),p2.getRank());
}
Notice how I've replace all the calls on p1 to calls on the current object.

choosing specific object in an array of objects

public abstract class ShapeClass {
private double area;
CONSTRUCTORS
MUTATORS, ACCESSORS
public abstract double calcArea();
}
public class CircleClass extends ShapeClass {
private int diameter;
private double area;
public CircleClass() {
super();
diameter = 10;
}
public CircleClass(CircleClass inCircle) {
super(inCircle);
diameter = inCircle.getDiameter();
}
public CircleClass(int inDiameter) {
setDiameter(inDiameter);
area = calcArea();
super.setArea(area);
}
public void setDiameter(int inDiameter) {
if(validateInt(inDiameter)) {
diameter = inDiameter;
} else {
throw new IllegalArgumentException("invalid diameter");
}
}
public int getDiameter() {
return diameter;
}
public boolean equals(int inDiameter) {
return(diameter == inDiameter);
}
public boolean equals(Object inObj) {
boolean same = false;
if(inObj instanceof CircleClass) {
CircleClass inCircle = (CircleClass)inObj;
if(super.equals(inCircle)) {
if(diameter == inCircle.getDiameter()) {
same = true;
}
}
}
return same;
}
public String toString() {
return (" area of circle is: " + super.toString());
}
private boolean validateInt(int inDiameter) {
boolean valid = false;
if (inDiameter>0) {
valid = true;
}
return valid;
}
private boolean validateReal(double inArea) {
boolean valid = false;
if(inArea>0.0) {
valid = true;
}
return valid;
}
#Override
public double calcArea() {
double radius;
radius = ((double) diameter) / 2.0;
area = Math.PI * radius * radius;
return area;
}
}
This is my code for a ShapeClass. I have two other classes Rectangle and Triangle, they're pretty much the same as the CircleClass.
In another class i'm assigning the ShapeClass objects in an array.
if I do that it'll be something like shape[3] = {Shape Object,Shape Object,Shape Object}. I don't know if that's right, I'm new to java. Sorry if there's any confusion.
My question is if I do that how do I distinguish what object is Circle, Rectangle or Triangle? When I want to print out a circle object only?
Thanks for the help.
You can check by using instanceof :
if(shape[0] instanceof Circle){
// do something
}
So there is an operator in java - instance of:
if(shapeObject instanceof Circle){
//print
}
so you can use it to distinguish objects by type. Also as for your question whether it's correct: You can use this approach with creating array of parent object type and putting children in it. After that, if you call toString method on each object from that array specific implementation of that method will be invoked. For example if there is Circle object in this array and there is overridden toString method in it then after calling toString on object from array of ShapeObject specific implementations will be invoked.
Try like this,
for(int i = 0; i < shapeArray.length; i++){
if(shapeArray[i] instanceof CircleClass){
// print circle here
}
}
You have 2 options:
// Solution 1: prits out all instances of Circle, basically also all subclasses of Circle
for (ShapeClass shape : shapes) {
if (shape instanceof CircleClass)
System.out.println(shape.toString());
}
// Solution 2: Matches exact class
for (ShapeClass shape : shapes) {
if (shape.getClass().equals(CircleClass.class))
System.out.println(shape.toString());
}
The above solutions will solve the task you asked about. But maybe the information below will be userful for you:
What if you want to print out the names of each shape, how to distingush them in this case?
Let's say we have 3 shapes:
public class Shape {
public void print() {
System.out.println("Shape is printed");
}
}
public class Triangle extends Shape {
#Override
public void print() {
System.out.println("Triangle is printed");
}
}
public class Circle extends Shape {
#Override
public void print() {
System.out.println("Circle is printed");
}
}
This code will print exactly what you need, because you defined the same function for all of the shapes, overriding it in child classes, and the appropriate function will be called based on object type determined at the runtime:
for (Shape shape : shapes) {
shape.print();
}

implementing own compareTo and compare Methods

im working on some old tasks given by my Programming course.
Im supposed to create a Rectangle class with some basic functions and a lot of Comparable and Comparator classes.
This is what I've got already:
public class Rectangle {
private int length, width;
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public int length() {
return length;
}
public int width() {
return width;
}
public int area() {
return length + width;
}
public int perimeter() {
return 2 * length + 2 * width;
}
}
public class RectangleComparable extends Rectangle implements Comparable<RectangleComparable> {
public RectangleComparable(int length, int width) {
super(length, width);
}
#Override
public int compareTo(RectangleComparable r) {
if (this.area() < o.area()) return -1;
else if (this.area > o.area()) return 1;
else return 0;
}
}
public class RectangleComparatorArea implements Comparable<RectangleComparable> {
private int area;
#Override
public int compareTo(RectangleComparable r) {
if (area < r.area()) return -1;
else if (area > r.area()) return 1;
else return 0;
}
}
public class RectangleComparatorPerimeter implements Comparable<RectangleComparable> {
private int perimeter;
#Override
public int compareTo(RectangleComparable r) {
if (perimeter < r.perimeter()) return -1;
else if (perimeter > r.perimeter()) return 1;
else return 0;
}
}
public class ComparableComparator<T> implements java.util.Comparator<T> {
public int compare(T o1, T o2) {
return 0;
}
}
What are methods like .compareTo, .equals etc. called? Like, when I got some
obj1.compareTo(obj2);
how can I refer obj1, when I implement my own compareTo method?
Also the ComparableComparator.compare method should work for Rectangles and also other Generic types but I got no idea how to start this.
this . That's the reference to the object whose compareTo get called.
See how jdk's implementation works: java.util.Comparators$NaturalOrderComparator (this is implementation detail and is hidden, tough)

Why am I getting this error about the compareTo method?

I am trying to understand the compareTo method. I wrote this class AboutcompareTo, but i am stuck in why/how i get this error?- the code is nearly finished.
anyone can explain in details what i am doing wrong. Thanks
the code:
public class AboutCompareTo {
public static void main(String[] args) {
Fruit[] fruits = { new Fruit(2), new Fruit(3), new Fruit(1) };
java.util.Arrays.sort(fruits);
}
}
class Fruit implements Comparable<Fruit> {
private double weight;
public Fruit(double weight) {
this.weight = weight;
}
#Override
public int compareTo(Fruit o) {
Fruit f = (Fruit) o;
if (Fruit > o.Fruit) // <-- the error
return 1;
else if ((Fruit < o.Fruit)) // <-- the error
return -1;
else
return 0;
}
}
The compareTo method compares an instance of your class, i.e. this Fruit, to an instance of another Fruit passed to you as a parameter. Therefore, the comparison needs to be between o's weight, and your own weight:
#Override
public int compareTo(Fruit o) {
if (this.weight > o.weight)
return 1;
else if (this.weight < o.weight)
return -1;
else
return 0;
}
Note 1: I used this.weight to refer to the weight of this Fruit. I did this to point out that weight attribute belongs to this instance; however, you can omit this. from the expression, i.e. use weight > o.weight instead.
Note 2: I assume that you did this for a learning exercise. For production code Java class library provides a pre-built method for comparing doubles - i.e. Double.compare. You can rewrite the method in a single line:
#Override
public int compareTo(Fruit o) {
return Double.compare(weight, o.weight);
}

Categories