• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals() says a Parent is equal to a Child

 
Arijit Daripa
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Arijit Daripa
    Ranch Hand
    Posts: 142
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.............
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic