• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals() says a Parent is equal to a Child

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.............
     
    Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic