• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

what does it mean

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.

What does this statement mean?

is it correct or not ?

means if an overridden method has declared an exception.It is not necessary for the overridding method to declare that exception .
if it is correct.
Why the following program is giving compile time error?

please explain me about this


class Animal {
public void eat() throws Exception {
// throws an Exception
}
}
class Dog2 extends Animal {
public void eat() { // no Exceptions }
public static void main(String [] args) {
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat();
a.eat();
}
}

C:\practice>javac Dog2.java
Dog2.java:16: unreported exception java.lang.Exception; must be caught or declar
ed to be thrown
a.eat(); // compiler error -
^
1 error
[ February 26, 2007: Message edited by: anil kumar ]
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,

Here most important thing is it is Compile time Exception.

It don't know the Object Animal referring to.
Just it know the An invocation of eat method on Animal reference will throw an exception.

Hope you go it
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please tell me the exception that you got.
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Srinivasan thoyyeti
but at compile time the compiler checks the methods which are present in the reference type so there is no problem
and also tell me about the statement which i wrote in top of the page



please tell me

and again i wrote the compile time error.

please check it priya.
[ February 26, 2007: Message edited by: anil kumar ]
 
Priya Viswam
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

means if an overridden method has declared an exception.It is not necessary for the overridding method to declare that exception




what you said is right. You can check that by commenting the code a.eat(). The program compiles fine. Animal eat() is throwing an exception but its subclass eat() method is not declaring that exception.


But when you call a.eat(), the compiler checks the methods that are in the reference type. and in that method it is declared as throwing the exception. So you have to follow the rule "either handle or declare".
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Priya Viswam , Srinivasan thoyyeti


Thanks

And Thanks to Priya viswam.


I am stupid .I think i am sleeping while i am reading the book.But any way thanks to you.
Since morning i am fighting with this question.I did not have my breakfast even.Now i will go and take the breakfast.
Bye
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,
A finishing touch needed here.
Be careful with the polymorphic assignments and general assignments.

1)Animal a = new Dog();
a.eat(); // error

2)Dog g = new Dog();
g.eat(); // guess what happens here. Then only you can take "break" fast.

Lets Rock
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Srinivasan thoyyeti


There is no problem there.Because the reference type is Dog ,so the compiler checks the Dog reference and i has find that one and after that at run time ,the object is also Dog type so it will execute the Dog overridden method

I think i am right
 
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic