aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interface problem 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interface problem" Watch "interface problem" New topic
Author

interface problem

payal sharma
Ranch Hand

Joined: Oct 04, 2000
Posts: 57


here object a1 is of tyep AnInterface but reference of AnInterfaceImpl.
Call method on this object is giving compile time error methodOne do not catch or throw Exception.
But why

this compiles and with out and error or exception.
George Pincharoen
Ranch Hand

Joined: Jul 03, 2001
Posts: 45
Hi,payal
In statement
AnInterface ai = new AnInterfaceImpl();
compile-time type of ai is AnInterface that methodOne() throws an
Exception if you not catch it compile-time error occur,
but statement
AnInterfaceImpl ai = new AnInterfaceImpl();
compile-time type is AnInterfaceImpl that methodOne() dosen't throws an Exception so it compiles without error
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
To do away with the problem we could have a1.methodOne() in a try catch block or we could simply use a throws with the main() in ATest .
Thanx , it cleared my doubt aswell .
Rest w/ George
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi,
Would like to add that it is always better to program to an interface rather than program to a class.The advantage you get is you can change the stub without any requirement for client compilation.This cannot be possible when you program to a class.
Hence,

AnInterface ai = new AnInterfaceImpl();

is better than

AnInterfaceImpl aii = new AnInterfaceImpl();

It is bad programming to implement the methodOne() without throwing an exception.Remember that the interface signifies responsibilities which every implementing class must adhere to.

Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited July 25, 2001).]


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
 
jQuery in Action, 2nd edition
 
subject: interface problem
 
Similar Threads
clarification on this Exceptioncode required
Interface
Interface questions
public modifier
please answer my query?