Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

what the ans is on earth?(scjp03-Q4)

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q4
You are writing a set of classes related to cooking and have created your own exception hierarchy derived from java.lang.Exception as follows
Exception
+-- BadTasteException
+-- BitterException
+-- SourException
Your base class, "BaseCook" has a method declared as follows
int rateFlavor(Ingredient[] list) throws BadTasteException
A class, "TexMexCook", derived from BaseCook has a method which overrides BaseCook.rateFlavor(). Which of the following are legal declarations of the overriding method?
a. int rateFlavor(Ingredient[] list) throws BadTasteException
b. int rateFlavor(Ingredient[] list) throws Exception
c. int rateFlavor(Ingredient[] list) throws BitterException
d. int rateFlavor(Ingredient[] list)
THE GIVEN ANS:acd.
I think the checked Exception which override method throws should be the same with supper class or the subclass Exception of the supper Exception ,concern the case the BitterException are not among the two case above ,so I think c is not right.
am I right?somebody correct me if I am wrong.
MY ANS:ad.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are writing a set of classes related to cooking and have created your own exception hierarchy derived from java.lang.Exception as follows
Exception
+-- BadTasteException
+-- BitterException
+-- SourException
The last means that BitterException is the subclass of the BadTasteException.
I think so
 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exception
+-- BadTasteException
+-- BitterException
+-- SourException

the diagram seems that the BadTasteException ,BitterException ,SourException are the subclass of the Exception.And they on the same-level,they are parallel.

 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If they are all at same level(having same superclass Exception)as you specify, then only correct options are a & d as you said. You are right. This can be verified from this code -

The compiler error that you get is -
"The method int rateFlavor(Ingredient[]) declared in class TexMexCook cannot override the method of the same signature declared in class BaseCook. Their throws clauses are incompatible."
BTW, whick mock test is this? you can suggest the site to change the correct answers.
HTH,
- Manish
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manish how can option d be correct when it is not throwing any exception at all?

Originally posted by Manish Hatwalne:
[B]If they are all at same level(having same superclass Exception)as you specify, then only correct options are a & d as you said. You are right. This can be verified from this code -

The compiler error that you get is -
"The method int rateFlavor(Ingredient[]) declared in class TexMexCook cannot override the method of the same signature declared in class BaseCook. Their throws clauses are incompatible."
BTW, whick mock test is this? you can suggest the site to change the correct answers.
HTH,
- Manish[/B]


 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roopa...
It is not compulsory for a overriding method to throw exceptions..But if it decides to throw any, it can only throw the exception(s)(including its subclasses) declared in the throws clause of the superclass's method or a subset(if any) of the above mentioned exception(s) and their subclasses(if any).
Hope this helps
Shyam
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! 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