Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Doubt in the output of this program.

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I don't understand why Mammal eats food is being displayed.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
h is of type Mammal when eat is called. Therefore, eat(Mammal m) runs.

Regards,
Dan
 
Ranch Hand
Posts: 89
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pinki,

The methods are basically overloaded and choice of which overloaded method to call or the signature depends upon
reference type and signature. If you see the method is taking Mammal reference so it is calling the Mammal eating
Hay. I guess this is the correct answer.

Arhaan
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arhaan Singhania wrote:Hi Pinki,

The methods are basically overloaded and choice of which overloaded method to call or the signature depends upon
reference type and signature. If you see the method is taking Mammal reference so it is calling the Mammal eating
Hay. I guess this is the correct answer.

Arhaan



Hi Arhaan

I dont see methods overloaded, eat method have only one parameter, they are overridden. but, i think, h is of type mamal, so mamal code is excuted. I did not execute the code though, just guessing.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mezan Shareef wrote:

Arhaan Singhania wrote:Hi Pinki,

The methods are basically overloaded and choice of which overloaded method to call or the signature depends upon
reference type and signature. If you see the method is taking Mammal reference so it is calling the Mammal eating
Hay. I guess this is the correct answer.

Arhaan



Hi Arhaan

I dont see methods overloaded, eat method have only one parameter, they are overridden. but, i think, h is of type mamal, so mamal code is excuted. I did not execute the code though, just guessing.



hi mezan i think that arhaan is right... if you read katy and bert book about rule of overriding, they said that :"The argument list must exactly match that of the overridden method. If they
don't match, you can end up with an overloaded method you didn't intend"
the problem here is this : Mammel is a parent of Cattle and cattle is the parent of Horse
so Horse inherit all eat() method from Mammel and Cattle
Cattle inherit eat() method from Mammel
so if you call the eat method from Horse object and pass a Cattle or Mammel reference , it will call the Cattle or Mammel eat method because Horse object inherit them.
regards.
 
Ranch Hand
Posts: 144
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mamal eats food because 'h' is of type Mammal so Mammal's class eat method is called.

Hope this helps.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hI PINKY ROY

Polymorphism does not work in Overloading functions and therefore it is decided at compile time which version to call of overloaded functions. And since the compiler is concerned only with reference type therefore it will see that you are calling the method with Mammal as an argument and thus it will call mammal version of the method. If you have some another query please post it here.
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I totally agree with foxy explanation. Hope this helps.
Thanks,
Arhaan
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mezan Shareef wrote:

Arhaan Singhania wrote:Hi Pinki,

The methods are basically overloaded and choice of which overloaded method to call or the signature depends upon
reference type and signature. If you see the method is taking Mammal reference so it is calling the Mammal eating
Hay. I guess this is the correct answer.

Arhaan



Hi Arhaan

I dont see methods overloaded, eat method have only one parameter, they are overridden. but, i think, h is of type mamal, so mamal code is excuted. I did not execute the code though, just guessing.


Methods are overloaded because although there is only one parameter but they are different.
 
Mezan Shareef
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

foxy milano wrote:

Mezan Shareef wrote:

Arhaan Singhania wrote:Hi Pinki,

The methods are basically overloaded and choice of which overloaded method to call or the signature depends upon
reference type and signature. If you see the method is taking Mammal reference so it is calling the Mammal eating
Hay. I guess this is the correct answer.

Arhaan



Hi Arhaan

I dont see methods overloaded, eat method have only one parameter, they are overridden. but, i think, h is of type mamal, so mamal code is excuted. I did not execute the code though, just guessing.



hi mezan i think that arhaan is right... if you read katy and bert book about rule of overriding, they said that :"The argument list must exactly match that of the overridden method. If they
don't match, you can end up with an overloaded method you didn't intend"
the problem here is this : Mammel is a parent of Cattle and cattle is the parent of Horse
so Horse inherit all eat() method from Mammel and Cattle
Cattle inherit eat() method from Mammel
so if you call the eat method from Horse object and pass a Cattle or Mammel reference , it will call the Cattle or Mammel eat method because Horse object inherit them.
regards.




Hi all, sory for replying soo late, i was busy last few days. I understood that method is taking Mammal reference so it calls that Sys.out. I agree. But i still do not no, why are you all saying, it is overloaded method. I am sure i might be wrong, but it has only one parameter so it is overridden, and not overloaded. I am still missing something, may be, because you all say it is overloaded method.

Overloaded Method: has multiple parameters,
OverRidden Methods: has same # of parameters.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mezan,

For Overloaded Method, there will be change in method signature
But in Overriding Method, there won't be any change.

See the below link you can understand easily:
http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Cheers,
Hari krishna
 
Mezan Shareef
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hari Krishna wrote:Hi Mezan,

For Overloaded Method, there will be change in method signature
But in Overriding Method, there won't be any change.

See the below link you can understand easily:
http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Cheers,
Hari krishna




Hi

Thankyou Hari Krishna, i got it.
 
Yes, my master! Here is the tiny ad you asked for:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic