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?

Questions by vishalanand_154

Editorial / Best Answer

bharathnav  

  • Member Since Oct-2008 | Oct 13th, 2008


"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.

Showing Answers 1 - 45 of 45 Answers

frustu

  • Aug 1st, 2008
 

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

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.

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.

vegetto

  • Aug 9th, 2008
 

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!!

bharathnav

  • Oct 13th, 2008
 

"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.

meghanalwa

  • Mar 4th, 2009
 

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.

  Was this answer useful?  Yes

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

inthiyaj

  • May 8th, 2009
 

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,

chintu2583

  • Dec 16th, 2009
 

class Super {
void foo() throws NullPointerException { System.out.println("Super"); } }
public class SupSub extends Super {
void foo() { System.out.println("SupSub"); }
public static void main( String[] argv ) { SupSub sup=new SupSub(); Super spr=new SupSub(); spr.foo(); // Output SupSub }
}

  Was this answer useful?  Yes

zshekhtm

  • Dec 13th, 2010
 

Creating a super-class reference to a sub-class, when one of the super-class methods can throw an exception, does not by itself produces a compile time error.
If, however, we use this super-class reference to call a method that for a super-class throws an exception while for a sub-class does not throw it, compiler will complain.

Here is a code that causes compile error:

import java.util.*;
public class sample_base {
    public void main_logic() throws Exception {
        return;
    }
    public static void main(String[] args) {
            sample_base x = new sample_derived();
            x.main_logic(); // comment this line and it compiles with no errors
            return;
    }
};
class sample_derived extends sample_base {
    public void main_logic() {
        return;
    }
};

>javac sample_base.java
sample_base.java:13: unreported exception java.lang.Exception; must be caught or declared to be thrown
                        x.main_logic();
                                    ^
1 error

The reason for a compiler to report an error in line x.main_logic() is that during the compilation compiler treats x.main_logic() as a call to a function of super-class that is capable to throw an exception. Since it is not caught, compilers reports an error.

  Was this answer useful?  Yes

techbuz

  • Dec 23rd, 2011
 

Throws exception in super class doesnt affest over-riding:code eg as follows:

Code
  1. public class Ride{

  2. void clay(){

  3.         System.out.println("am in base class");

  4.        

  5. }

  6.  

  7.         public static void main(String[] args) throws Exception  {

  8.         Ride r=new Sride();

  9.         r.clay();

  10.  

  11.         }

  12. }

  13. class Sride extends Ride{

  14.         void clay()

  15.         {

  16.                 System.out.println("am in sub class");

  17.         }

  18. }


Output:
am in sub class

  Was this answer useful?  Yes

sampra

  • Mar 6th, 2012
 


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

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions