• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question: how to call a method ......

 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am asking as to how to be able to call a method from a given class, specially if the method is not a static one. please read the post below. i need to know how to call the method "equal" from the class Object. that is my question. details are below.
-------------------------------------------
String s1 = "Java"; String s2 = "Java";
boolean b1 = s1.equals(s2);====>>the equals method from the class String is called here.
is there a way to call the method equals from the class Object. (i know one could use the == as well, but what i am asking is actually how to be able to call a method from a different class.) If equals had been a static method then one could have called it
via the Object.equals call. but equals is not a static method. so is there a way to call it programmatically ?
or no ? not possible as s1 is a reference to the String class. But String is also a subclass of class Object. Now can we call the method "equals" from the class Object ?
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark -
1) Slow down. Ask one question at a time. Then ask a follow-up if you need more information.
2) Formatting counts. If you make the message easier to read, more people are likely to consider it.
3) A class may make it possible to call a method it has overridden, but you cannot do this directly with object references. The whole point of encapsulation is to let the class programmer control the object reference's view of available controls.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited December 30, 2001).]
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. let me reword my question.
i have two string objects s1 and s2.
boolean b1 = s1.equals(s2);====>>the equals method from the class String is called here.
Question :
is there a way to call the method equals from the class Object ? (we know that String class a subclass of Object class.)

Originally posted by Michael Ernest:
Mark -
1) Slow down. Ask one question at a time. Then ask a follow-up if you need more information.
2) Formatting counts. If you make the message easier to read, more people are likely to consider it.
3) A class may make it possible to call a method it has overridden, but you cannot do this directly with object references. The whole point of encapsulation is to let the class programmer control the object reference's view of available controls.

 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just try ((Object) aString).equals
this will call equals in object class. But because this is an overriden method the derived version will be called
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way to do that. You cannot upcast to invoke parent (overridden) behavior on an object reference; this behavior would have to appear in the method code itself. The whole point of dynamic method invocation is to execute behavior that is "closest" to the instance of the thing in memory.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic