• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question related to overridding

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I have come across this question while reading material given by friend


What is the result of compiling and running this program?



1. prints "Mammal eats food"
2. prints "Cattle eats hay"
3. prints "Horse eats hay"
4. Class cast Exception at runtime.


Ans.1

Can any one explain why not the overloaded version in Horse class--void eat(Horse h) was called and "Horse eats hay " was output???

I know that c.eat(h) has parameter whose reference type is Mammal type but how the rule of overridin was overlooked
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nitude gupta wrote:Hi I have come across this question while reading material given by friend



Can you be more specific about the material given by your friend. You need to tell the actual name of the mock exam or the related site...
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nitude gupta wrote:Hi I have come across this question while reading material given by friend


What is the result of compiling and running this program?



1. prints "Mammal eats food"
2. prints "Cattle eats hay"
3. prints "Horse eats hay"
4. Class cast Exception at runtime.


Ans.4

Can any one explain why not the overloaded version in Horse class--void eat(Horse h) was called and "Horse eats hay " was output???

I know that c.eat(h) has parameter whose reference type is Mammal type but how the rule of overridin was overlooked



 
nitude gupta
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,
I know quoting the source is important but I really don't know the sorce as I have this material in word files.
I will definetly post the source as soon I confirm
 
nitude gupta
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi deeps,
sorry for posting wrong answer actually output is "Mammal eats food"
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this code void eat is overloaded in each class (not overriden, because the parameter is not EXACTLY the same in each declaration).
Overloaded method are choosen at compile time.
When you call c.eat(h), c is a reference Cattle, which has access to
- void eat(Cattle c) {}
- void eat(Mammal m) {} through inheritance.
Your h is a Mamal reference so the compiler choose the void eat(Mammal m).

Covariance works with return type of method, not parameters.
 
nitude gupta
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I am wrong please correct me,if we change parameter type from super to subtype passed in method just like return type does it change signature
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have modified your example a bit, Now this class is overriding the method in each class.
Because in over-riding the method, the method signature remains the same. Now it will
give the expected output, you are mentioning above.

class Mammal{
void eat(Mammal m){
System.out.println("Mammal eats food");
}
}
class Cattle extends Mammal{
void eat(Mammal c){
System.out.println("Cattle eats hay");
}
}

class Horse extends Cattle{
void eat(Mammal h){
System.out.println("Horse eats hay");
}
}
public class Test{
public static void main(String[] args){
Mammal h = new Horse();
Cattle c = new Horse();
c.eat(h);
}
}

Hope this helps,
Ben
 
Ben Zaidi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nitude gupta wrote:If I am wrong please correct me,if we change parameter type from super to subtype passed in method just like return type does it change signature



Yes, it will change the method parameter type. Because co-variant concept is basically applicable in case
of return types, not the parameters, passed to the method. I suppose

Ben
 
nitude gupta
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys it was really helpful
reply
    Bookmark Topic Watch Topic
  • New Topic