• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

overiding method(own code)  RSS feed

 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

RESULT:compilation fails due to line 3
method on line 2 has been overrided ,ok
in overriding ,object type determines which method has to be invoked,so in line3 method in class B should be invoked,which is not throwing exception,i don't think line3 should be in try catch block.
Then why compilation fails?please explain the logic behind this.
 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At compile time, the compiler checks the reference type, not the actual object! So, a Compilation error!
 
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:At compile time, the compiler checks the reference type, not the actual object! So, a Compilation error!



Abimaran is correct. Since the compiler checks the reference type while compiling, it gives error with a.foo() because it throws Exception and it is not caught any where.
If you look closely, even if you add try catch block around line 3, it will NOT solve the issue. foo() method of Class A is still declared to throw an Exception.
So either you have to add a try/catch inside main() where a.foo() is called or you need to declare
for the code to compile.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply,tomorrow morning is my exam.really sacred.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arjun Srivastava wrote:thanks for the reply,tomorrow morning is my exam.really sacred.



Don't worry. All is well!
BTW, I've SCWCD exam tomorrow~!
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:
Don't worry. All is well!
BTW, I've SCWCD exam tomorrow~!


yea,AAL IS WELL.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey mate all is well!
 
Have you no shame? Have you no decency? Have you no tiny ad?
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!