aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding / overloading 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 "overriding / overloading" Watch "overriding / overloading" New topic
Author

overriding / overloading

Richard Huang
Greenhorn

Joined: Oct 02, 2000
Posts: 26
I cannot see why the answer should be A. Can somebody give me some help?

Given:
1. class Super {
2. public float getNum() { return 3.0f; }
3. }
4.
5. public class Sub extends Super {
6.
7. }
Which method, placed at line6, causes compilation to fail?
A. public void getNum() { }
B. public void getNum(double d) { }
C. public float getNum() { return 4.0f; }
D. public double getNum(float d) { return 4.0d; }
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
Originally posted by Richard Huang:
I cannot see why the answer should be A. Can somebody give me some help?

Given:
1. class Super {
2. public float getNum() { return 3.0f; }
3. }
4.
5. public class Sub extends Super {
6.
7. }
Which method, placed at line6, causes compilation to fail?
A. public void getNum() { }
B. public void getNum(double d) { }
C. public float getNum() { return 4.0f; }
D. public double getNum(float d) { return 4.0d; }

First, see that Sub extends Super. Since the method in super is public, Sub inherits that method. Now, if you want to override that method you would do something like choice C. However, because this method is inherited, if you want to overload it, remember that changing the return type is insufficient to overload a method. The parameter list needs to be changed atleast. Therefore, having known the 2 above things, it can't be C because that is a proper override, and it can't be B or D because those are proper overloads. What we have left is A because it is not a valid overload. Hope that helps! Good luck as you prepare for the exam!


SCJP 1.4<br />SCJD 1.4
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
Daniel's explanation is absolutlely correct. Another way to look at this, however, is to view this from the language designers' point of view.

If the above compiled, then we could write a driver program such as the following:

The problem here should not be in the driver program. When var holds a Sub instance, the getNum() method returns void. The println() call in these cases is broken.

If the Sub code compiles, we push the error from a compiler error easily caught at design-time to a run-time exception hiding in waiting when Super is extended. This is the language designers' reasoning for disallowing overriding on a return type.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
There is a difference between overloading and overriding.

The rule of gold says that you cannot have two methods with the same signature, that's to say: the same name and parameters type and order.

So this is valid:



But this is not:



Why not? Because the return type is not part of the method signature and the compiler will interpret that you declared the same method twice.

Sometimes you can overload a method that your class inherited from the parent class. Let�s see:



In the code above the method toString is overloaded from the Object class. However this would not be valid:



Because the method has the same signature as the one declared in the class Object, the return type is not enough to alter the signature.

Now an overriding is when you declare a method with the same signature,return type and compatible exceptions with those of the method declared in the parent class.

That means that although in the example above I redeclared the toString method as I altered its signature the method is not overriden, but overloaded.

When I overload a method then the instances of this class with method overloaded when calling the method will execute the code in the overloaded method and not one in the parent class. Of course the overloaded method can call the code in the parent class by means of the keyword super.



I hope it helps
 
Don't get me started about those stupid light bulbs.
 
subject: overriding / overloading