GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 492 of 493    Print  
Overriding Methods
Suppose a Super Class method throws an exception and this method is Overriden in the subclass with no exception. Now if you create a super class reference that has a subclass object. This throws a compile time error, Why?


  
Total Answers and Comments: 7 Last Update: October 13, 2008     Asked by: vishalanand_154 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: vegetto
 
The answer to this question is pretty obvious that you need to supply the mean or the way to handle the exception either by declaring in the method itself or you should supply the catch statement or handle it in the caller method or the class that is calling the super class method and when you compile it again it would go fine.

cheers!!

Above answer was rated as good by the following members:
sohanonline, Ramesh Israni, veeraraghavane, mrafi007
August 01, 2008 02:56:33   #1  
frustu Member Since: August 2008   Contribution: 1    

RE: Overriding Methods
Sub class's method will compile successfully as subclass's method can either specify the exception or not in its signature.But if its specifying it should be that exception,any of the sub classes of exception or any runtime exception. Sub class method can 't add any new exception
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 2Overall Rating: -N/A-    
August 07, 2008 03:08:19   #2  
midhagaurav Member Since: August 2008   Contribution: 1    

RE: Overriding Methods

While compiling such a program a base class reference always check for the compatibility for the base class method which is overridden in derived class as in future it may contain base class object for which methods calling should be verified for error checking.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    
August 07, 2008 23:24:05   #3  
r.praveenkumar Member Since: October 2007   Contribution: 26    

RE: Overriding Methods
Just now consider the method in super class do not throw any exception If we are assigning the object of subclass to super class reference and if we invoke the method using that reference the compiler will check whether that method is available in the super class since we are invoking the method only using the super class reference and if it is there then the prog successfully compiles but during execution JVM runs only the sub class version of the method. this is polymorphism.
 
Is this answer useful? Yes | No
August 09, 2008 02:37:30   #4  
vegetto Member Since: June 2008   Contribution: 10    

RE: Overriding Methods
The answer to this question is pretty obvious that you need to supply the mean or the way to handle the exception either by declaring in the method itself or you should supply the catch statement or handle it in the caller method or the class that is calling the super class method and when you compile it again it would go fine.

cheers!!

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    
August 22, 2008 07:45:51   #5  
Karuna Reddy Member Since: June 2007   Contribution: 40    

RE: Overriding Methods
sub class exceptions will fire first than super class exceptions
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
September 01, 2008 09:20:45   #6  
erhansrajpareek Member Since: September 2008   Contribution: 1    

RE: Overriding Methods
Because at run time, the super class will compiled first, so it will gives the run time error.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
October 13, 2008 13:03:18   #7  
bharathnav Member Since: October 2008   Contribution: 2    

RE: Overriding Methods
"Suppose a Super Class method throws an exception and this method is Overriden in the subclass with no exception. Now if you create a super class reference that has a subclass object. This throws a compile 
Latest Answer: Because at run time, the super class will compiled first, so it will gives the run time error. ..."

if the object has been created to the subclass and both the methods are non static then the method we overriden doesn't produce any errors or exceptions.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape