I edited your subject so it isn't in all caps. On the internet, all caps implies shouting.
As far as your questions, note that isBiped() is a static method. This means that inheritance/overriding does not apply. That's why both methods print false in the first one, but not the second.
[OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
-
1
-
-
-
-
Jeanne Boyarsky wrote:
As far as your questions, note that isBiped() is a static method. This means that inheritance/overriding does not apply. That's why both methods print false in the first one, but not the second.
Jeanne... I think that you may have mixed the examples. With overriding, since there is only one object, both method calls should go to the same method. So, the second case (non-static) should be the same. And the first case is the one that is different.
Henry
Henry Wong wrote:
Jeanne Boyarsky wrote:
As far as your questions, note that isBiped() is a static method. This means that inheritance/overriding does not apply. That's why both methods print false in the first one, but not the second.
Jeanne... I think that you may have mixed the examples. With overriding, since there is only one object, both method calls should go to the same method. So, the second case (non-static) should be the same. And the first case is the one that is different.
Henry
That I did. Have a cow for noticing. The logic in my explanation si right. For the static methods, there is no inheritance.
[OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
rochelle roger wrote:
and how is isBiped() actually invoked in the two codes, based on what is the decision made ?
With static methods, the method called is based on the type of the reference used to specify the method (as determined at compile time obviously). As Jeanne mentioned there is no polymorphism with static methods.
So, in this example, the methods are called based on the type of the this reference (implied from instance methods), which are, of course, based on the location of the definition of the instance method.
Henry
Henry Wong wrote:To answer the original question...
rochelle roger wrote:
and how is isBiped() actually invoked in the two codes, based on what is the decision made ?
With static methods, the method called is based on the type of the reference used to specify the method (as determined at compile time obviously). As Jeanne mentioned there is no polymorphism with static methods.
So, in this example, the methods are called based on the type of the this reference (implied from instance methods), which are, of course, based on the location of the definition of the instance method.
Henry
Henry,
1. the this reference would mean joey right. and type of joey is Kangaroo . why do you say that type of "this" reference will be the location of the definition of the instance method.
2. why static methods dont participate in inheritence? that means static members are never inherited ?
rochelle roger wrote:
1. the this reference would mean joey right. and type of joey is Kangaroo .
No. I mean the this reference. This is the reference that is accessible by instance methods, and gives it access to the instance itself. The usage of the this reference is also implied when you call a method without a reference, like in your example with the call to the isBiped() method.
Henry
rochelle roger wrote:
2. why static methods dont participate in inheritence? that means static members are never inherited ?
To start, I am really hesitant in answering here because I am not sure if you understand all the subtleties yet... but I guess I can qualify everything a bit first.
When you say "inheritance", do you mean polymorphism? ... which is a big part of overriding, has an effect on the behavior of methods, and is related to this topic?
Or do you mean that the subclass having access to the superclass's members? ... which is what is generally meant, when discussing whether fields (members) are inherited?
Anyway... No. Static methods are not polymorphic. Fields, either instance or static, are not polymorphic. As for why, well, for static fields and methods, it is kinda impossible to be polymorphic when the type of the instance (assuming that one even exist) is not known, isn't it?
Henry
how does isBiped() get called from the parent class in the static case, but from the child class in the other case?
when isBiped() is static, you spoke about the "this" keyword
i have read that when "this" is used, it refers to the current instance which is joey
and since we have a static method here, itll take the reference type of joey which is Kangarro the child class.
then why is the parent class iBiped called
so how does it choose the parent class then ?
in short, how is the parent class chosen in the static case and how is the child class chosen in the non static case
rochelle roger wrote:
i have read that when "this" is used, it refers to the current instance which is joey
and since we have a static method here, itll take the reference type of joey which is Kangarro the child class.
A couple of things....
One. There is a difference between a reference variable and an object (that it refers to). These are different, and quite frankly, they may have different types. You need to understand the distinction here.
Two. There are multiple references in the example. And the references are not the same. The this variable may reference the same object as the joey reference variable, but that doesn't make the two variables the same (actually, three, as there are multiple different this reference variables in this example).
To understand which static method will be called, you first need to determine which reference variable is used, and then, determine the type of that variable.... it is definitely *not* ... "oh, that reference variable points to the same things as joey, so it it is a Kangaroo".
Henry
rochelle roger wrote:
in short, how is the parent class chosen in the static case and how is the child class chosen in the non static case
This is a different question, as you as asking the difference between a static and non-static method (for the latter, where polymorphism is happening).
Whether the method is static or not, the method to be called is initially determined at compile time. The compiler will figure out the type of the reference being used (or if the Class type is directly specified), and then, generate code that calls that method. For static methods, it doesn't automatically go to the parent -- it depends on how it is used.
For instance (non static) methods, there is also a runtime step. The code that is generated doesn't directly call the method of the class (that is determined at compile time). Instead, it goes to do an indirect method call (which with the current implementations, is done via a jump table). This table is filled in at runtime, and it is based on the actual type of the object. So, if the instance is the child class, it will go to the override method.
Henry
now all that i want to know is ,
1. when a static method isBiped() was called from getMarsupialDescription(), why is the Marsupial class' method called
2. when instance method isBiped() was called from getMarsupialDescription(), why is the Kangaroo class' method called
rochelle roger wrote:
now all that i want to know is ,
1. when a static method isBiped() was called from getMarsupialDescription(), why is the Marsupial class' method called
2. when instance method isBiped() was called from getMarsupialDescription(), why is the Kangaroo class' method called
Response from a previous post...
Henry Wong wrote:
Whether the method is static or not, the method to be called is initially determined at compile time. The compiler will figure out the type of the reference being used (or if the Class type is directly specified), and then, generate code that calls that method. For static methods, it doesn't automatically go to the parent -- it depends on how it is used.
For instance (non static) methods, there is also a runtime step. The code that is generated doesn't directly call the method of the class (that is determined at compile time). Instead, it goes to do an indirect method call (which with the current implementations, is done via a jump table). This table is filled in at runtime, and it is based on the actual type of the object. So, if the instance is the child class, it will go to the override method.
Henry
rochelle roger wrote:henry if you dont mind can you explain with the help of the example i've given !
because what you're saying is very general.
Yea, perhaps you can elaborate a bit on what you are confused with. At this point, I can't tell if you actually understand the concepts or not. And if you do somewhat understand the concepts, I can't tell where are you confused that you can't map the concepts to the example / explanations so far.
Henry
-
2
-
-
-
-
let me try to explain you, based on your example.
The difference between the two code snippets is that you have static methods isBiped() in the snippet 1 and instance methods isBiped() in the snipped 2.
A static method is also known as class method. Which means, that it belongs to a class and doesn't need an instance of the class (an object) to be called. So you can write something like Marsupial.isBiped() or Kangaroo.isBiped() and it will work.
An instance method needs an instance of the class. So you must first create an instance with new Marsupial() or new Kangaroo() to be able to call the method isBiped() on the object reference.
Coming back to our two code snippets. They both create an object new Kangaroo() and call instance methods getMarsupialDescription() and getKangarooDescription() on the reference variable joey. It is possible because the class Kangaroo extends Marsupial, so it inherits the method getMarsupialDescription() from it.
What happens in the snippet 1:
this method makes a system out and calls the method isBiped() on the class Marsupial because this is the class method in the same class, where the method getMarsupialDescription() is defined!
this method makes a system out and calls the method isBiped() on the class Kangaroo because this is the class method in the same class, where the method getKangarooDescription() is defined
What happens in the snippet 2:
this method makes a system out and calls the method isBiped() on the reference of type Kangaroo because it is an instance method and it was overridden by the method isBiped() in the class Kangaroo
this method makes a system out and calls the method isBiped() on the reference of type Kangaroo because this is the class method in the same class, where the method getKangarooDescription() is defined