This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Invoking Object superclass's method outside of subclass when the method is overriden?
Ajit Sawant
Ranch Hand
Joined: Nov 26, 2008
Posts: 33
posted
0
Hello, In the code below, I would like to execute Object class's equals() method. How I can I do that? By default Carx will execute the overriden equals() method.
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
Don't override equals() in Carx.
SCJP 6
Ajit Sawant
Ranch Hand
Joined: Nov 26, 2008
Posts: 33
posted
0
I need to override equals()method in Carx as I'm testing the concept of overriding Object class's equals() method. After overriding, I want to know how to call Object class's equal() method.
Ankit is correct. Also note that this is not different from the usual rule that says that you can't call an instance method from a static context. Although super is not a method (it is a keyword,) if you are going to call an instance method of the superclass via super, you have to do it in an instance context.
Additionally, both these syntaxes are correct: super.<methodName>(); <ClassName>.super.<methodName>();
Again, this only works from within an instance method. [ December 31, 2008: Message edited by: Ruben Soto ]
All code in my posts, unless a source is explicitly mentioned, is my own.
Ajit Sawant
Ranch Hand
Joined: Nov 26, 2008
Posts: 33
posted
0
Thanks for the suggestion. I did super.equals(b) and also testequalobjx.super.equals(b) But by doing that it will not compare two Carx objects but compare the object testequalobjx to a Carx object using testequalobjx' equals(). I looking for something that uses Object class equals() and compare two Carx objects. Carx a object.object class' equal method (Carx b object) -- Also it will not run the Object class' equals() method if the superclass of the testequalobjx also has overriden the equals() method. I have another interesting question on deserialization. I have posted that with subject as "Serialization: Non serialized class issue". I'll appreciate if you guys can comments on that too.
[ December 31, 2008: Message edited by: Ajit Sawant ]
How does it help to call the Object class's equals method?Doesn't that defeat the point of overriding it?Won't the objects just be compared using == ?
===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
Ya you are right Duran, calling Object' equals() will compare using ==.
Ajit just want to taste what will happen when he will call Object's equals(), and how it is different then Ajit's overriding equals().
No it does not defeat the point of overriding, as there is no syntax to call super class method, if you want to call you can only call it in your subclass' instance method.
like this: public boolean equalsCar(Object o){ return super.equals(o); }
Here I have made one instance method so that it can call super.InstanceMethod() method.
Oh because super only works from an instance context? I don't understand the point of this exercise [ January 02, 2009: Message edited by: Duran Harris ]
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
Ya super is counterpart of this. this means current object, super means current object' super class.
There is nothing great to understand in this program. Ajit has overriden equals() method and testing his equals() method, but he also wants to call superclass's Object equals() method at the same time. But he was not able to do this, not able to find how to use super construct to call Object's equals() method, thats it.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Invoking Object superclass's method outside of subclass when the method is overriden?