File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ajit Sawant
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't override equals() in Carx.
 
Ajit Sawant
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call super.equals to call the overridden method. But you can do that only in non-static methods...
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ajit Sawant
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do this way buddy
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 == ?
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 608
Eclipse IDE Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic