Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using 'this' in a parameter of a method call

 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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){

}
}
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic