get specs using this 3,5 2,4
get specs without using this 3,5 2,4
Please don't say parent class and child class, even though many people do. Say superclass and subclass (or subtype). Maybe the C# terms, base class and derived class, are better.Anil Philip wrote:. . . a method defined only in the parent class . . .
What sort of members? Do you mean fields with the same name? Please use the correct terminology; that will make it much easier to get good answers.members with the same name in both . . . classes.
If RocketShip extends Ship, then that instance is a Ship instance. It is an instance of both types.Anil Philip wrote:. . . It is a RocketShip instance, not a Ship. . . .
Not quite. If you are in nested types, then there may be more than one example of this.There is only one 'this'. Is that statement correct? . . .
Campbell Ritchie wrote:
It is usually a bad idea to have fields with the same name in a superclass and a subclass..
Campbell Ritchie wrote:
If RocketShip extends Ship, then that instance is a Ship instance. It is an instance of both types.Anil Philip wrote:. . . It is a RocketShip instance, not a Ship. . . .
Not quite. If you are in nested types, then there may be more than one example of this.There is only one 'this'. Is that statement correct? . . .
Stephan van Hulst wrote:First, polymorphism only applies to non-private instance methods. It doesn't apply to fields. So when the Ship class refers to weight or height, it will always be the weight or height field that is a member of the Ship class, not the field that is declared in the RocketShip class. Fields also don't override each other: RocketShip.weight is a completely different and unrelated field to Ship.weight.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote: Any method in Ship class cannot see variables defined in any sub-class. Also public int getWeight() { return weight; } and public int getWeight() { return this.weight; } mean the same thing at runtime (in this case where there is no variable in the method which shadows the class field).
Stephan van Hulst wrote:Again, the 'this' keyword is almost exactly like a method parameter. And just like any other variable, it has a 'compile-time type'.
Take a look at this example:
The 'that' parameter does in fact refer to an object with type RocketShip. However, the field that is accessed depends only on the compile-time type of the object reference. It doesn't matter whether 'that' in getWeightOfThatShip() actually refers to a RocketShip or another type of Ship. The 'that' parameter has compile-time type Ship, and therefore that.weight will refer to Ship.weight, not RocketShip.weight.
Now, replace 'that' with 'this' and the application will work exactly the same way.
getWeightOfThatShip this.weight 3
Anil Philip wrote:When I step through it in the debugger, I see that this.weight is the value in RocketShip (see screenshot) this.weight = 2
But when I output the value, it outputs the value in Ship. Confusing.
Stephan van Hulst wrote:
Let that be a lesson: IDEs often use labels that may be confusing
In this case, the 'this' label in your IDE is really just that: A textual label that refers to the current object. It doesn't really work like the actual 'this' keyword.
Consider Paul's rocket mass heater. |