Forums Register Login

Class Hierarchy nightmares

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
Water! People swim in water! Even tiny ads swim in water:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 751 times.
Similar Threads
Class using Main without declaring itself Public
Another question about Widening and Boxing
weird overloading?
Overriding Clarification
Overloading For int and long ????
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:30:46.