This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Class Hierarchy nightmares

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that the runtime type of an object is used when accessing methods, as is true when the following code is run (the result is "2"):



Now when we change the input parameter on the super class to one that does not exactly match the argument being sent in, the super class method is invoked and the result is "1":
class FWD {
int doMud(long x) { return 1; }
}
class Subaru extends FWD {
int doMud(int y) { return 2; }
}
class Race {
public static void main(String [] args) {
FWD f = new Subaru();
System.out.println(f.doMud(7));
}
}

I am obviously missing something here as I do not understand why the JVM does not continue to use the subclass method which defines an input parameter type that exactly matches the type coming int (int to int). Why is it that when the parameter type on the super class changes to a long that the JVM decides to use the super class method?

Thanks again for your help
g
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because of you are changing parameter list in subclass, so now it is overloaded method. Which method is called at compile time is depend on the reference type in overloaded method. So now it is calling the super class method.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marshall,

Here we need to talk about the differences between Method Overriding and Method Overloading. In the initial case what we did is method Overriding and in the second case what happened is because of method overloading.

Method Overriding, is basically used to provide our own implementation for the method inherited from Parent. For that to work, below are the few rules to be followed

1) Method needs to be inherited.
2) Should have the same signature as that of parent for the method under consideration.
3) Method arguements should exactly match (This point is very important)
4) Return type should match or atleast should be satisfying IS-A relation.
5) Should not declare any new Checked exceptions
6) Should not have more restrictive access modifiers.

Above are the few important rules to be followed for method over riding which happened in our first case.

Coming to the second case, our rule 3 is not followed, which resulted in Method overloading in which arguments should NOT match .

In the case of Method Overriding, Object at is decided at run time and in case of method overloading it is decided at compile time.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class FWD {
int doMud(int x)
{ return 1; }}
class Subaru extends FWD {
int doMud(int y) { return 2; }}
class Race {
public static void main(String [] args) {
FWD f = new Subaru();
System.out.println(f.doMud(7)); }}

output: 2

class FWD {
int doMud(long x)
{ return 1; }}
class Subaru extends FWD {
int doMud(int y) { return 2; }}
class Race {
public static void main(String [] args) {
FWD f = new Subaru();
System.out.println(f.doMud(7)); }}

output:1
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic