Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic