Returning false from Equals methods without Overriding

后端 未结 3 855
隐瞒了意图╮
隐瞒了意图╮ 2021-01-29 13:46

Write TestEquals class so that main method of Puzzle3 class prints false.

Note: You cannot override equals method o

相关标签:
3条回答
  • 2021-01-29 14:00

    You may not override the equals method, but there is no reason you cannot overload the equals method.

    The method Object.equals has prototype:

    public boolean equals(Object o) { ... }
    

    To override this method you have to create a method with the same prototype in TestEquals. However your problem statement indicates that you are not allowed to override this method. No problem, overloading the method is a valid approach to accomplish your task. Simply add the following method definition to TestEquals:

    public boolean equals(TestEquals o) { return false; }
    

    And you're done.

    0 讨论(0)
  • 2021-01-29 14:06

    Following would print false :)

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.io.UnsupportedEncodingException;
    
    public class TestEquals {
    
      public static void main(String[] args) {
        TestEquals testEquals = new TestEquals();
        System.out.println(testEquals.equals(testEquals));
      }
    
      static {
        System.setOut(new CustomPrintStream(new PrintStream(System.out)));
      }
    
      public static class CustomPrintStream extends PrintStream {
    
        /**
         * This does the trick.
         */
        @Override
          public void println(boolean x) {
          super.println(!x);
        }
    
        public CustomPrintStream(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
          super(file, csn);
        }
    
        public CustomPrintStream(File file) throws FileNotFoundException {
          super(file);
        }
    
        public CustomPrintStream(OutputStream out, boolean autoFlush, String encoding)
          throws UnsupportedEncodingException {
          super(out, autoFlush, encoding);
        }
    
        public CustomPrintStream(OutputStream out, boolean autoFlush) {
          super(out, autoFlush);
        }
    
        public CustomPrintStream(OutputStream out) {
          super(out);
        }
    
        public CustomPrintStream(String fileName, String csn) throws FileNotFoundException,
                                                                     UnsupportedEncodingException {
          super(fileName, csn);
        }
    
        public CustomPrintStream(String fileName) throws FileNotFoundException {
          super(fileName);
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-29 14:20

    Instead of overriding equals you can overload equals

    class TestEquals {
        // a common mistake which doesn't override equals(Object)
        public boolean equals(TestEquals te) { 
              return false;
        }
    }
    
    0 讨论(0)
提交回复
热议问题