Categories
Java

On Java Interfaces and Exception

While implementing an abstract factory pattern for my software engineering class I noticed something that seemed weird to me: I defined an interface with several methods, each method was declared to throw several exceptions using the standard throws keyword.

When I created the implementation class for that method, I noticed that I do not have to throw all of the exceptions the interface specified. Actually, I only needed to throw one exception for the method I was overriding for the specific implementation and Java let me. I tried researching more on the relationship between exceptions and interfaces finding nothing.

Turning into the real Java bible, the Java Language Specification (I recommend the free download), I encountered the following sentence in the spec (page 299):

The throws clause of an overriding method may not specify that this method will
result in throwing any checked exception which the overridden method has not permitted, by its throws clause, to throw. When interfaces are involved, more than one method declaration may be overridden by a single overriding declaration. In this case, the overriding declaration must have a throws clause that is compatible with all the overridden declarations.

So an implementing or overriding method (from an interface or a super class) may only throw exceptions that are among the ones the interface or super class specified in the original method declaration. By converse, this means that if an overriding method does not need to throw exceptions that are thrown by the interface or super class’ method, it does not have to. In other words:

An overriding method can throw any of the exceptions the original method throws, or none of them.

Share
Categories
Computing Java

Java private is not so private

I am sure that many of us feel very warm and safe in knowing fundamental facts about Java. For example, a class variable like

private int h;

cannot be modified by any object other than the class it exists in, right?
Wrong – otherwise why would I waste my time here? The issue popped up when I was writing my solution to a class assignment and I noticed that instances of the same class can access each other’s private member variables.
Further investigation on the Java Tutorial reminded me of the very fundamental fact that the private access modifier specifies class visiblity, NOT instance visiblity. Definitely not something anyone would recommend using, just something you should keep in mind. Always.

Share
Categories
Java

Unable to compile class for jsp means just that!

So last night one of my students’ final projects just refused to work. I rebuilt it, tweaked the Ant script, nothing worked! 3 hours. I am tired. I repeatedly get the unable to compile class for jsp error on one of his JSPs.
Finally, I read a posting online and try to run JBoss using JDK 1.4.2.whatever instead of the 1.5 JDK, and guess what? Worked like a charm! The issue had to do with one of the struts tld files, so maybe that is a conflict I was definitely unaware of…

Share
Share