Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes equals() says a Parent is equal to a Child Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals() says a Parent is equal to a Child" Watch "equals() says a Parent is equal to a Child" New topic
Author

equals() says a Parent is equal to a Child

Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142


Any object which IS-A Line can pass the instanceof test in line 1. Any obect of the following class Rectangle will pass the instanceof test in line 1.



Now if a Rectangle object has the same Length(Instance Variable) as a Line object, the second euality test in line 1 will evaluate true, hence true is returned from the equals() method of a Line object.



Hence, a Line object with length 3 is same as a Rectangle object with length 3 and breadth 2.

I don't know what Object Orientation says. But, whatever does it say, is it correct?

I want two different objects evaluates false in equals() method, anyhow, without using Generics or any higher order supporting stuff.

I am trying.
Please somebody help.....................
[ August 14, 2008: Message edited by: ARIJIT DARIPA ]

SCJP 5
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

you need to implement/override the equals() function in your classes.

The current code calls super() for rectangle with the length argument, so a Rectangle is a Line, and the value is the same (3) in your example.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
Do a search for Joshua Bloch's Effective Java; either buy a copy or find the sample chapter on the Sun website. There is a lot more about the equals() method in there.

And I hope you are not trying to say that a Rectangle IS-A Line.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You need to override equals:

Don't forget to override hashCode as well! Read the API of Object to see why.

This code still has one flaw though - it violates one rule of the Object.equals contract: it's not symmetric.

Consider the following:

This is why I almost always code my equals methods as follows:

I only use instanceof if one of the following occurs:
  • The class is final. There can be no subclass, so only objects of the class itself will pass the instanceof test
  • The equals method is final. Since it can't be overridden, subclasses cannot use their own instanceof.



  • I do have one question about your design though. Is a Rectangle really a Line? It can be, but not the way you have represented it. With your definition of length and breadth, a Rectangle has Lines, but it isn't one.
    Now a Rectangle can be a line (with corners), but then length will be the 4 sides added, and not just the length of one size.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Arijit Daripa
    Ranch Hand

    Joined: Aug 09, 2008
    Posts: 142
    Originally posted by Rob Prime:
    You need to override equals:
    .
    .
    .
    .


    Sorry verybody!
    I couldn't recall getClass().
    So it can do rest of the job.
    Thanks Rob. Thanks a lot............
    And thanks to Campbell and Pat also.............
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: equals() says a Parent is equal to a Child
     
    Similar Threads
    array index out of bound problem
    Overriding equals() and hashCode()
    Rectangle Class Assignment-need help
    Overriding equals()
    classes and applications