• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Overloading Vs. Overriding [From "Jeanne Boyarsky" OCAJP book]

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the following code sample from "Jeanne Boyarsky" OCAJP book:

Filename: Bird.java

Filename: Eagle.java


Following is the statement written in this book and I do not understand the highlighted part:-

The second method, eat(), is overridden in the subclass Eagle, since the signature is the
same as it is in the parent class Bird- they both take a single argument int. Because the
method is being overridden, the return type of the method in Eagle must be a subclass of
the return type of the method in Bird
. In this example, the return type of void is not a sub-
class of int. therefore, the compiler will throw an exception on this method definition.
 
Author
Posts: 42
7
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does not have to be a subtype; consider it a small typo. Look at the Rule #4 at the top of the same page 248:

If the method returns a value, it must be the same or a subclass of the method in the parent class, known as covariant return types.

You can check this rule by changing the declaration of the method eat() in Bird by replacing void with int; just add an appropriate return statement --- and you'll see that the class Eagle will compile. Also you can add the @Override annotation to eat() in Eagle, and the compiler won't object to it.

HTH.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic