• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding / overloading

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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; }
 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic