Inheritance , method signature , method overriding and throws clause

前端 未结 4 1150
半阙折子戏
半阙折子戏 2021-02-02 12:56

My Parent class is :

import java.io.IOException;
public class Parent {
        int x = 0;
        public int getX() throws IOException{
        if(x         


        
相关标签:
4条回答
  • 2021-02-02 13:02

    No, this is appropriate - an overridden method can be more restrictive about what it throws (and returns) because this can be useful for callers who know at compile time that they'll be using the overridden method, and don't want to bother with exceptions which can't happen etc. It has to be more restrictive rather than more permissive though, so that it can't surprise callers who do access it through the parent declaration.

    Using the overridden method via a reference of type Parent is never going to violate the contract of "it might throw IOException" - the absence of an exception doesn't violate the contract. The other way round (if the parent didn't declare the exception, but the overriding method does) would be a contract violation.

    0 讨论(0)
  • 2021-02-02 13:03

    Well, the overriding method might not throw any exception at all (or at least fewer exceptions), thus you can remove exceptions from the throw clause (or the throw clause as a whole).

    Suppose the overriding method catches all exceptions, logs them and returns a special value. Although that's not good style (it would change the semantics of the method) it is still possible andd thus you'd not have to catch exceptions that are never thrown if you know at compile time that you're dealing with a Child.

    Adding exceptions won't work, since users of the class who access it through the Parent reference don't know of any exceptions that Child might add.

    0 讨论(0)
  • 2021-02-02 13:15

    Easy to remember

    1. Access modifier can be changed from restricted to less restricted,
      for example from protected to public, but not vise versa
    2. throws signature can be changes from parent exception to child exception class, but not vise versa

    This code is valid

    public class A  {
    
        protected String foo() throws Exception{
            return "a";
        }
    
        class B extends A {
            @Override
            public String foo() throws IOException{
                return "b";
            }
        }
    }
    

    Overrided foo method has public access, not protected and throws IOException that child of Exception

    This code is not valid

    public class A  {
    
        public String foo() throws IOException{
            return "a";
        }
    
        class B extends A {
            @Override
            protected String foo() throws Exception{
                return "b";
            }
        }
    }
    

    Overrided foo method has more restricted access modifier and throws Exception that NOT child of IOException

    By the way you can override method from superclass and not throw ecxeptions at all

    This code valid

    public class A  {
    
        public String foo() throws IOException{
            return "a";
        }
    
        class B extends A {
            @Override
            public String foo(){
                return "b";
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-02 13:22

    You have the freedom to override a method without the throws keyword, because if you want to develop a method by controlling all the exceptions, then you can do that by overriding the method without any throws clause.

    But remember one thing that if you want to include the throws clause in the subclass method, then the throws clause must associate with the exception which must be the same or the subclass of the exception what is thrown by its superclass method. for example-

    class Super{
        void a()throws IOException{
            ......
        }
    }
    class Sub extends Super{
        void a()throws IOException{
            ......
        }
    }
    

    The a() method of class Sub must throw IOException or any subclass of IOException, otherwise compiler will show error.

    That means if you write

    void a()throws Exception
    

    in the class Sub, then it will cause compilation error.

    0 讨论(0)
提交回复
热议问题