aspose file tools*
The moose likes Beginning Java and the fly likes Throws, in inheritance ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Throws, in inheritance !" Watch "Throws, in inheritance !" New topic
Author

Throws, in inheritance !

Prathamesh Gaddam
Ranch Hand

Joined: Feb 18, 2008
Posts: 58
Hi,

1. Please clarify why the inherited/overriding method in the sub-class can skip the 'throws' for an exception, though vice versa is compile error. Refer code.

2.Does the overriding/inherited method need to only throw the same exception or it specialized exception, because in the following e.g if the class B throws 'Throwable', compile error.



3. Towards above example, please clarify in accordance to these statements:
i) A a = new B(); a.process(); ERROR
ii) B b = new B(); b.process(); COMPLIES

Thank you in advance !
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Using your example, consider what happens when you have a method that returns an A:


However since A and B are both types of A, the getMyA() method can legally return a B. The calling code doesn't really know or care what the runtime class the getMyA() method returns, as long as it can be used like any other A.


Since a subclass of A should be able to be used anywhere an A can be used then it follows that:
  • An overriding method in a subclass can always throw fewer checked exceptions than the method it overrides.
  • An overriding method in a subclass can never throw more checked exceptions than the method it overrides.

  • For additional reading on this subject, google Liskov Substitution Principal


    Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
    amitabh mehra
    Ranch Hand

    Joined: Dec 05, 2006
    Posts: 98
    Originally posted by Prathamesh Gaddam:
    3. Towards above example, please clarify in accordance to these statements:
    i) A a = new B(); a.process(); ERROR
    ii) B b = new B(); b.process(); COMPLIES

    Thank you in advance ![/QB]


    Rule:When a subclass overrides a method from super class, it can only specify all or a subset of the exception classes thrown in the overridden method of the super class.

    If this restriction was not there, then in your i) case, while compiling, compiler checks the process() method of A for exceptions since the reference type is of A type. But the process() in A says that an exception is to be thrown. So it will be a breach of contract at runtime.

    Hence the check during compile time.
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Google for the Liskov Substitution Principle - that is describing the whole reason for this behavior.


    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Throws, in inheritance !
     
    Similar Threads
    why does this compile?
    overriding doubt
    Overriding doubt in K&B
    Over-riding method which throws exception