polymorphic supertype reference exceptions

Posted on 2011-05-04
Last Modified: 2012-05-11

I was reading folowing lines and I did not understand clearly.
If a method is overridden but you use a polymorphic supertype
reference to refer to the subtype object with the overriding method, the compiler
assumes you are calling the supertype version of the method. If the supertype version
declares a checked exception, but the overriding subtype method does not, the compiler
still thinks you are calling a method that declares an exception
Any ideas, resources,sample code,links, highly appreciated. thanks in advance
Question by:gudii9
    LVL 47

    Accepted Solution


    But they have a very good example there:
    for you should not catch exception because
    eat() method of Dog class does not throw exception

    for you  should  catch exception ,because
    compiler assumes you are calling a method of class Animal
    even though a was instantiated as Dog, so compiler will
    requuire that you catch Exception

    class Animal {
      public void eat() throws Exception {
        // throws an Exception
    class Dog2 extends Animal {
      public void eat() { // no Exceptions }
      public static void main(String [] arga) {
        Animal a = new Dog2();
        Dog2 d = new Dog2();;           // ok;           // compiler error -
                           // unreported exception

    Open in new window

    LVL 92

    Assisted Solution

    that question is really well covered here

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now