This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes To Reference the Interface Or the Class That Implements the Interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "To Reference the Interface Or the Class That Implements the Interface? " Watch "To Reference the Interface Or the Class That Implements the Interface? " New topic
Author

To Reference the Interface Or the Class That Implements the Interface?

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I have some methods defined in an interface UserDAO; something like:

and I have a class MySQLUserDAO that implements UserDAO.
If I want to access the method findUserPassword( String username ) from a class called UserService, I should access the interface or the class that implements the interface? For example, which one is correct:

or

[ November 24, 2003: Message edited by: JiaPei Jen ]
Uma Balu
Ranch Hand

Joined: Aug 22, 2003
Posts: 61
Hi,
Generally, we will create only a reference to an interface. Your code,

will not compile because, you are trying to instantiate an interface, which is not possible.
May be, you can use it this way,

Thanks,
Uma.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Interfaces are acting as superclasses of the implementing class, because Java doesn't allow multiple inheritence... So the usage of interfaces is devoloped in Java...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To Reference the Interface Or the Class That Implements the Interface?
 
Similar Threads
Spring DAO: When and Where to catch/throw exceptions
How to use httpsession with dao factory
Struggling With Passing Data Between Data Access Class, Business Bean, and Controller
Data Access Objects: static methods
I can't get the struts working