File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class Hierarchy nightmares Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class Hierarchy nightmares" Watch "Class Hierarchy nightmares" New topic
Author

Class Hierarchy nightmares

Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
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


G
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
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.


SCJP-1.5<br />SCWCD-1.4
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
praveen oruganti
Ranch Hand

Joined: Feb 05, 2007
Posts: 73
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


Regards,<br />Praveen Oruganti<br />Software Programmer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class Hierarchy nightmares