This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question: how to call a method ...... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question: how to call a method ......" Watch "question: how to call a method ......" New topic
Author

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

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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

Joined: Oct 25, 2000
Posts: 7292

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).]


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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

Joined: Jul 03, 2001
Posts: 2120
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


SCJP2. Please Indent your code using UBB Code
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question: how to call a method ......
 
Similar Threads
confused about inheritance
Doubt in equals method
Why doesn't the subclass variable hide it?
one more with Strings ( == and equals)
String Comparison