aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Invoking Object superclass's method outside of subclass when the method is overriden? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Invoking Object superclass Watch "Invoking Object superclass New topic
Author

Invoking Object superclass's method outside of subclass when the method is overriden?

Ajit Sawant
Ranch Hand

Joined: Nov 26, 2008
Posts: 33
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
Don't override equals() in Carx.


SCJP 6
Ajit Sawant
Ranch Hand

Joined: Nov 26, 2008
Posts: 33
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 Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

You can call super.equals to call the overridden method. But you can do that only in non-static methods...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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
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 ]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
You can do this way buddy
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

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
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.
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

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
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://aspose.com/file-tools
 
subject: Invoking Object superclass's method outside of subclass when the method is overriden?