Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Why is it that the generic equals method must accept a parameter of the type object?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is it that the generic equals method must accept a parameter of the type object?

    Hi there I have been wondering about the generic equals method in java for a while.

    And by the generic equals method I mean this method:

    public boolean equals(Object o) {
    		if(o instanceof <type>) {
    			<type> other = (<type>) o;
    			// compare the data and return the result.
    		} else {
    			return false;
    		}
    	}

    So why is that this method must accept a parameter of the type object?

    To me it is pretty obvious that two objects of different type are not equal.

    So is it to make the method more flexible, meaning that it can accept parameters of any type,
    or is it to overwrite the equals method all objects inherit from the Object super class?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Why is it that the generic equals method must accept a parameter of the type object?

    That equals method is inside the Object class which is the top level class in Java. The Object class has no idea about any other classes that exist* so how can it have a method that takes a parameter with a type it does not know about?

    * In fact when the Object class was written no other classes did exist.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Why is it that the generic equals method must accept a parameter of the type object?

    In Java a method only overrides if it matches the same input types exactly.

    class Base
    {
        public boolean equals(Base b)
        {
            System.out.println("base equals");
            return false;
        }
    }
     
    class Child extends Base
    {
        public boolean equals(Child c)
        {
            System.out.println("child equals");
            return true;
        }
    }

    Here, equals(Child) cannot possibly override equals(Base) because there's the possibility someone will call the function with a Base object (there's no "selective" overriding based on type).

    void do_it(Base b1, Base b2)
    {
        b1.equals(b2); // there's no way to know at compile time if b1 and b2 are Child objects
                            // only safe behavior is to call Base.equals(Base)
    }

    This obviously is not the desired behavior for polymorphic objects, thus the only logical solution is to allow equals to be called with a common base class. It was decided long ago in Java history that there would be one common base class Object. As a consequence, all Objects are comparable to other Objects and it's the users prerogative to do runtime type checking.

    You can define your own equals method which takes a specific type, just realize that it won't behave polymorphically with base classes.

    class A
    {
        public boolean equals(Object obj)
        {
            if(obj instanceof A)
            {
                return this.equals((A)obj);
            }
            return false;
        }
     
        public boolean equals(A obj)
        {
            // ...
        }
    }

Similar Threads

  1. create an equals() method that overrides the object equals() method
    By slinky29 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2013, 11:11 PM
  2. Method calculate 2 object using constructor with parameter
    By DavidXCode in forum Object Oriented Programming
    Replies: 2
    Last Post: November 21st, 2012, 09:31 PM
  3. Creating Method that returns a casted object type
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: October 31st, 2012, 01:23 PM
  4. How to convert an array of integer to generic type?
    By colerelm in forum Object Oriented Programming
    Replies: 2
    Last Post: October 20th, 2012, 04:08 AM
  5. Replies: 3
    Last Post: May 27th, 2012, 11:11 AM