public class Dog { private String name; public Dog(String name){ this.name = name; } public boolean equals(Object other){ Dog d1 = (Dog) other; return this.name.equals(d1.name); } public static void main(String[] args) { Dog d1 = new Dog("Rufus"); Dog d2 = new Dog("Sally"); Dog d3 = new Dog("Rufus"); Dog d4 = d3; System.out.println(d1.equals(d2)); System.out.println(d2.equals(d3)); System.out.println(d1.equals(d3)); System.out.println(d3.equals(d4)); } }
equals
true
false
1 Dog d1 = (Dog) other; 2 return this.name.equals(d1.name);
Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class Dog
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)