• Post Reply Bookmark Topic Watch Topic
  • New Topic

overriding and hiding

 
rochelle roger
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CAN SOMEBODY PLEASE explain the reason in difference of output in the first and second code snippets ?

snippet 1




snippet 2




and how is isBiped() actually invoked in the two codes, based on what is the decision made ?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36007
422
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rochelle,
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.
 
Henry Wong
author
Sheriff
Posts: 22619
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36007
422
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 22619
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
rochelle roger
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Henry Wong
author
Sheriff
Posts: 22619
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 22619
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
rochelle roger
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no im just asking, when we invoke a static isBiped() from an instance method, and in the other case when we invoke  a non static isBiped() from the instance method,

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

 
Henry Wong
author
Sheriff
Posts: 22619
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 22619
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
rochelle roger
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok all that you said is fine !

why is there no polymorphism with static methods?
 
rochelle roger
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i get that as to why polymorphism doesnt work with  static methods !

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
 
Henry Wong
author
Sheriff
Posts: 22619
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 22619
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Viktor Logwin
Ranch Hand
Posts: 84
10
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi rochelle,

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
 
rochelle roger
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay Viktor thanks !!!     

thanks to you too henry !   
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!