Forums Register Login

Using 'this' in a parameter of a method call

+Pie Number of slices to send: Send
The parameter to the getValue method is a Base object reference. The Robinwood class extends Base, and when the keyword 'this' is used as the parameter to the getValue method, I am wondering if it is referring to 'the parentclass of which this class belongs' ?
BTW - In the code below, the second method 'main' (which i presume to be an overloaded name?) calls the method getValue. Now, the method getValue in the paentclass Base is abstract (and as such must have no body), and it is 'implemented' so to speak, in the Robinwood class by having a pair of curly braces added to it.
abstract class Base{
abstract public void getValue(Base b);

}
public class Robinwood extends Base{
public static void main(String argv[]){
Robinwood rw = new Robinwood();
rw.main();
}
public void main(){
getValue(this);
}
public void getValue(Base b){

}
}
+Pie Number of slices to send: Send
Dear Jasper ,
According to my understanding, you were wondering whether this keyword is being used as a parent class reference. But in reality it is not. As Robinwood class inherits from Base class, a Robinwood object reference can be passed for Base reference. There will be runtime casting.
if you want to be sure of what is being passed and what is "this" in this code, i have added a line which prints out the reference that refers to "this". Check this code . Compile and run it and you will know

Hope this helps
Sri
+Pie Number of slices to send: Send
Nice one Sri!
So the arg is expecting an object of class Base, but it gets sent an object of class Robinwood. An implicit cast occurs and everything is okay, because a subclass can be upcast to the parent class.

ps - thanks for the code, nice one.
+Pie Number of slices to send: Send
class RobinWood is-a base class
It's override the getValue() method in the base class .when invoking ,somthing occur~~
such mechanism is called polymorphysm
---
Robbies
-----------------------------
1.java IDE tool : JawaBeginer
2.Java Jar tool : JavaJar
http://www.pivotonic.com
+Pie Number of slices to send: Send
it is not so much that fact that the getValue method has been overridden by Robinwood class, but that fact that it takes an object of type Base, and just wanted to check what the parameter of (this) was giving.
Seems the method is given a reference to a Robinwood object which is cast to a Base object (which is perfectly legal since RObinwood extends Base).
[ January 19, 2003: Message edited by: Jasper Vader ]
Looky! I'm being abducted by space aliens! Me and this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 730 times.
Similar Threads
why do we get 40?
Abstract Classes
exam qs- object orientation- help!
is it valid to declare an inherited method as abstract?
using this
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:26:48.