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.
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.
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com