wood burning stoves*
The moose likes Beginning Java and the fly likes Using 'this' in a parameter of a method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Watch "Using New topic
Author

Using 'this' in a parameter of a method call

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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){

}
}


giddee up
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
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
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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.
Robbie shi
Greenhorn

Joined: Jan 05, 2003
Posts: 28
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
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using 'this' in a parameter of a method call