Win a copy of Terraform in Action this week in the Cloud 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Overriding question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, a question about overriding.

in K&B "exam watch" page 104 i compile ok. why?

And later, page 111, at table 2-3 it say:

exceptions Can reduce or eliminate in overridden methods.

Can anybody explain me this?

Thank you!
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following situation

Class A{

public void method1() throws IOException{
System.out.println("class A")
}

}

Class B extends A{

// this class throws subclasses of IOException
public void method1() throws FileNotFoundException{
System.out.println("class B")
}

}

Class C extends A{

// this method throws less exceptions
public void method1(){
System.out.println("class C")
}

}



- Class A defines method1 as throwing IOException
- Any class that extends class A (here B or C) that overrides method1 must throw the same exceptions, less exceptions or subclasses of the original exception.
- Class B does not throw the original IOException from class A, but throws FileNotFoundException (which is a subclass of IOException) - it could also have thrown ZIPException which is also a subclass of IOException
- Class C chooses to rather throw less exceptions (none actually).

Basically you can not throw more exceptions in an overridden method because logic which has based itself on the original method would not cater for the extra exceptions.

Hope this helps.

Chris
 
javier berbel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. But I don't know why the K&B book say in page 104:

"This code will not compile because of the Exception declared on the
Animal eat() method. This happens even though, at runtime, the eat() method used would be the Dog version, which does not declare the exception."

Can you help me about is?

Thanks

Javier
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Since the object ( a ) used to invoke the eat method is "Animal" type and "Animal" class eat throws an exception, we must enclose it in try-catch block. Thats the only reason y the code will not compile.

Asha
 
javier berbel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

thank you very much!


This solved my doubt.


Javier
 
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