Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why does this compile?

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


isnt this an illegal way of overriding?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh.....i forgot that....sorry i got it why it compiles...
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why the above code gives compile error???
can anyone explain please??
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because at compile time the compiler knows only about the Hotel class.....compiler thinks you would call Hotel's method whch declares to throw a checked exception....which shoudl be caught....which isnt in your code....so bang ...compile error

clear hai?
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur is right, you have to handle exception at main by either declaring it or catching it
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes its cleared..
then what about the above TryIt class?? why its compilng??
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is arule that states that the overriden method in subclass can either decalre less exceptions or no exceptions
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seems you are new...read the Kathy siera book once
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
k Thanks Ankur.
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the rule ankur.. TryIt class compiles because of at comile time the compiler knows about B class so it is compiling..
am i right???
correct me if i am worng??
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup...right..bang on target
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually the compiler knows abt b so it isnt compiling
 
Chandana Garlapati
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur kothari wrote:actually the compiler knows abt b so it isnt compiling


What i said...
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the rule ankur.. TryIt class compiles because of at comile time the compiler knows about B class so it is compiling..
am i right???
correct me if i am worng??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic