GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 492 of 502    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: 11 Last Update: May 20, 2009     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, raghu.emails, ahamza59, minakshi sharma, snigdhapbabu, ae8166558de88
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
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 4   Answer is not useful 5Overall Rating: -1    
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 4Overall Rating: -3    
August 07, 2008 23:24:05   #3  
r.praveenkumar Member Since: October 2007   Contribution: 32    

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 | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    
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 5Overall Rating: -4    
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 3Overall Rating: -3    
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 3Overall Rating: -3    
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 3   Answer is not useful 1Overall Rating: +2    
March 04, 2009 01:42:53   #8  
meghanalwa Member Since: March 2009   Contribution: 1    

RE: Overriding Methods
Super-class and Sub-class have method of same name.
The programe compile easily but at run time it decide which method to be loaded. Weather sub-class or super-class. This concept is called as method over-riding.

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
April 09, 2009 23:28:51   #9  
sudhakarbaish Member Since: April 2009   Contribution: 1    

RE: Overriding Methods
According to Java compiler in case of over ridding always follow following rules: If a method of super class throws an exception then overridden version of that method in child class (sub class) should also throw same set of exceptions & should be properly handled.
If you agree vote me
sid

 
Is this answer useful? Yes | No
May 08, 2009 23:25:41   #10  
inthiyaj Member Since: May 2009   Contribution: 4    

RE: Overriding Methods
In this case we are calling a method with super class reference and sub class object. Super class reference can call only the methods which it knows (its knowledge) and execute sub class's defined method. So while we are calling method Super class has the knowledge that this method will throw an exception and need to handle this. Because of this the compiler shows error.
Thanks

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape