473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Super equals

Hi,

I have a class Range with 2 fields (starts and ends), it overrides equals() method:
Expand|Select|Wrap|Line Numbers
  1. public boolean equals(Object o) {
  2.         if(this == o)
  3.             return true;
  4.         if(o == null)
  5.             return false;
  6.         if(! (o instanceof Range))
  7.             return false;
  8.         Range r = (Range) o;
  9.         if(this.starts.equals(r.starts) && this.ends.equals(r.ends))
  10.             return true;
  11.         return false;
  12.     }
The class is extended by 2 other classes - Gap and Task which does not overrides equals().

In this example Task equals Gap with the same starts and ends fields... :/.

What are the solutions (besides overriding equals method in Task and Gap or comparing getClass() (if(! (this.getClass().equals(o.getClass()))) return false) in the Range.equals() method) to avoid it?

Thanks in advance,
Etam.
Sep 23 '08 #1
3 2997
JosAH
11,448 Expert 8TB
There are no alternatives besides (partially) overriding the equals() method in
the two sub classes or handling it all in the super class equals() method.

kind regards,

Jos
Sep 23 '08 #2
(...) or handling it all in the super class equals() method (...)
Do you mean:
Expand|Select|Wrap|Line Numbers
  1. if(! (this.getClass().equals(other.getClass())))
  2.  return false;
  3.  
?

Isn't it ineffective?

Thanks,
Pawel.
Sep 24 '08 #3
JosAH
11,448 Expert 8TB
Do you mean:
Expand|Select|Wrap|Line Numbers
  1. if(! (this.getClass().equals(other.getClass())))
  2.  return false;
  3.  
?

Isn't it ineffective?

Thanks,
Pawel.
Three method calls; the most expensive (probably) being the equals() method.
You can't do much better than that in your little three class hierarchy.

kind regards,

Jos
Sep 24 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Kerim Borchaev | last post by:
Hello! Always when I use "super" I create a code duplication because class used as first arg to "super" is always the class where the method containing "super" was defined in: ''' class C:...
2
by: Clarence Gardner | last post by:
The super object is considered a solution to the "diamond problem". However, it generally requires that the ultimate base class know that it is last in the method resolution order, and hence it...
11
by: Nicolas Lehuen | last post by:
Hi, I hope this is not a FAQ, but I have trouble understanding the behaviour of the super() built-in function. I've read the excellent book 'Python in a Nutshell' which explains this built-in...
0
by: Michele Simionato | last post by:
Here is an idea for a nicer syntax in cooperative method calls, which is not based on Guido's "autosuper" example. This is just a hack, waiting for a nicer "super" built-in ... Here is example...
0
by: Delaney, Timothy C (Timothy) | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286195 This is a new version of super that automatically determines which method needs to be called based on the existing stack frames....
6
by: Steven Bethard | last post by:
When would you call super with only one argument? The only examples I can find of doing this are in the test suite for super. Playing around with it: py> class A(object): .... x = 'a'...
7
by: Kent Johnson | last post by:
Are there any best practice guidelines for when to use super(Class, self).__init__() vs Base.__init__(self) to call a base class __init__()? The super() method only works correctly in multiple...
9
by: Mike Krell | last post by:
I'm reading Alex Martelli's "Nutshell" second edition. In the section called "Cooperative superclass method calling", he presents a diamond inheritance hierachy: class A(object): def...
4
by: ddtl | last post by:
Hello everybody. Consider the following code: class A(object): def met(self): print 'A.met' class B(A): def met(self):
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.