View Full Version : Help in java!
12-04-2011, 03:18 AM
Hi guys, i need help understanding interface and exceptions. I really do not understand the idea behind it. Can someone explain using a example? It will be really nice!
12-04-2011, 04:13 PM
Interfaces and exceptions are completely distinct in what they need to do. There is no direct relationship between the two as there is between a class and and interface (logically that is, Exceptions can certainly implement Interfaces).
Interfaces are simply a contract that guarantees typeof Interface will have x method available to use. Any class implementing an interface will inherit its datatype as well and this is the only way to perform multiple inheritance in Java (and also why interfaces are 1000x more useful than a base extends).
Exceptions are thrown when the program doesn't know what to do with data provided or how to deal with bad returns. Its easiest thought of as a 'pass the buck' type idea - I don't know what to do with this, so you figure it out. The File will throw a FileNotFoundException if the path is bad. But it doesn't know what you want to do with it, so you try/catch it when calling file, and may ask for input again on a file. Maybe its a configuration file instead, so you print an error and terminate the application.
Exceptions can be checked or unchecked. Checked exceptions force the caller to try/catch or further send the caller by marking the method as checked and adding the throws XException to it, effectively making the caller of that method responsible for dealing with it. This can be done indefinitely, but really should be try/caught no higher than main. Above main, the VM will instead catch the exception and toss a fatal termination of the program.
Unchecked do not require a preemptive try/catch. Without one though, if the exception is tossed it will terminate the program.
Also, in the future please select a title more suiting to the question you are asking or problem you are having.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.