Comparing Dog and Person

an anonymous user · January 09, 2022 · Java
import java.util.*;
import java.lang.*;
import java.io.*;

class Dog {
  public String name;
  public Dog (String name) {
    this.name = name;
  }
}

class Person {
  public String name;
  public Person (String name) {
    this.name = name;
  }
}

class Main {
    public static void main(String[] args) {
        
        Dog dog = new Dog("Fido");
        Person person = new Person("Fido");
        
        // System.out.println(dog == person); // error: incomparable types: Dog and Person
        System.out.println(dog.equals(person)); // false
        System.out.println(person.equals(dog)); // false
        
    }
}

Comments

Please sign up or log in to contribute to the discussion.