Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8236627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:11:36+00:00 2026-06-07T19:11:36+00:00

Can a method in a subclass override a method in the parent class and

  • 0

Can a method in a subclass override a method in the parent class and throw a run time exception when the method in the parent class throws no exception? Something like this:

class X { public void foo() { System.out.print("X "); } }

public class SubB extends X {
    public void foo() throws RuntimeException {
        super.foo();
        if (true) 
            throw new RuntimeException();
        System.out.print("B ");
    }

    public static void main(String[] args) {
        new SubB().foo();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:11:40+00:00Added an answer on June 7, 2026 at 7:11 pm

    Yes, because runtime exceptions aren’t part of the method signature. (You can add them but the compiler doesn’t care, they’re just documentation.)

    I think the rationale is that RuntimeExceptions are usually programmer errors, like NPEs or going out of bounds of an array, so it doesn’t make sense to try to restrict them the way that checked exceptions are restricted. You can’t make a rule saying, this method (and any method overriding it) can never ever throw a NullPointerException, because the JVM doesn’t make those kinds of guarantees.

    In the Java Language Specification under Compile-Time Checking of Exceptions it says:

    The unchecked exception classes (§11.1.1) are exempted from
    compile-time checking.

    Of the unchecked exception classes, error classes are exempted because
    they can occur at many points in the program and recovery from them is
    difficult or impossible. A program declaring such exceptions would be
    cluttered, pointlessly. Sophisticated programs may yet wish to catch
    and attempt to recover from some of these conditions.

    Of the unchecked exception classes, runtime exception classes are
    exempted because, in the judgment of the designers of the Java
    programming language, having to declare such exceptions would not aid
    significantly in establishing the correctness of programs. Many of the
    operations and constructs of the Java programming language can result
    in exceptions at run-time. The information available to a Java
    compiler, and the level of analysis a compiler performs, are usually
    not sufficient to establish that such run-time exceptions cannot
    occur, even though this may be obvious to the programmer. Requiring
    such exception classes to be declared would simply be an irritation to
    programmers.

    For example, certain code might implement a circular data structure
    that, by construction, can never involve null references; the
    programmer can then be certain that a NullPointerException cannot
    occur, but it would be difficult for a Java compiler to prove it. The
    theorem-proving technology that is needed to establish such global
    properties of data structures is beyond the scope of this
    specification.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a parent-child that defines some method .foo() like this: class Parent
I would like to know how can I override indexOf() method in a subclass
Can this method work? public String sayHello(){ return Hello.jsp?name= + laala; } I am
What tool or method can I use to see what code something like an
Can someone explain to me why @Override public void fooMethod(Class<?> c) doesn't override public
I would expect subclass always call super() for some method. How can I enforce
Which method can be used to read one line at a time from a
in C# an internal class or method can only be called from within the
If i want to override the Thread#start() method can I? and if yes will
I still can't figure out how to make a subclass constructor follow its parent...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.