File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overiding method(own code) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overiding method(own code)" Watch "overiding method(own code)" New topic
Author

overiding method(own code)

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


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.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

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.

SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

thanks for the reply,tomorrow morning is my exam.really sacred.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Jun 23, 2010
Posts: 432

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

yea,AAL IS WELL.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Hey mate all is well!
 
Consider Paul's rocket mass heater.
 
subject: overiding method(own code)
 
Similar Threads
Doubt in Nested Inner Class
Initialisation steps
Urgent help needed.How to skip a LOC execution in method def
How to skip execution of a line in a java method defition.Urgent help needed ranchers
how to skip execution of a line in a method..need help ranchers