aspose file tools*
The moose likes Java in General and the fly likes Interface as a parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interface as a parameter" Watch "Interface as a parameter" New topic
Author

Interface as a parameter

sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
Hi there,
Is the following code correct:
Public class test{
public test (InterA itera)
{
...
}
}
InterA is an interface. If it's correct, why
and how to use it?
Thanks in advance,
Sarah
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
It is not only correct, but also is recommended.
How to use it?
When you make the method call, you must pass a concrete implementation of the interface to it.
Here is an example with explanation in JDK itself (abstract class):
Q. Why abstract method Graphics.drawString() can work without implementation?

There are more in Java collection framework.

Thanks!
Roseanne
Join our Study Group when certified
sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
Thanks a lot, Roseanne.
BUT
How can I pass a concrete implementation of the interface to
the method? I can not tell it by the example you showed me.
Sarah
---
"When you make the method call, you must pass a concrete implementation of the interface to it."
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Here is an running example:
<pre>
interface IFace{
void drawFace();
}
class RealFace implements IFace {
public void drawFace(){
System.out.println("I'm drawing a beautiful face");
}
}
public class TestFace {
public static void callIFace(IFace face) {
face.drawFace();
}
public static void main(String[] args) {
IFace face = new RealFace();
callIFace(face);
}
}
</pre>
However, I think that you were posting on the wrong forum. It is a beginner question. CHEATING!!
Thanks!
Roseanne
sarah Marsh
Ranch Hand

Joined: Mar 06, 2001
Posts: 282
What the design pattern of this usage?
dexter ferreira
Greenhorn

Joined: Feb 16, 2001
Posts: 7
The design pattern for this usage is programming by interfaces, where you base a lot of programming on interfaces, so that the underlying implementations can be easily modified, without affecting you current code.
Originally posted by sarah Marsh:
What the design pattern of this usage?

Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Using the interface as the parameter is no particular 'pattern' at all. It's a technique that takes advantage of the inheritance principle.
Outside of design patterns like Singletons and other nifty tricks, virtually every Design Pattern out there uses interfaces as parameters to decouple the design of the pattern from the implementation.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
I agree with Michael, it is more a programming style/discipline than a design pattern.
In a popular saying, "High cohesion, less coupling."
An example is JAXP1.1, it defines a lot of interfaces, the real implementation will depends on the vendor. If you code it right, and only against the interface, any parser implements JAXP1.1 should work fine. If you switch the default parser (e.g. xerces to crimson or vice versa) on the commend line or your .bat/.sh file, user should not notice any differences. However, if your look into the vendors' jar files, your will be shocked by the huge implementation differences.
However, this is an ideal case. When you really switch, you will certainly need some adjustments by using the least commom denominators (strictly coding according to the interface). One reason is that programmers might use some undocumented features of one parser, which is totally different for another parser. Another reason would be the parser has a bug.
"All softwares are bug free until you find one."

Thanks!
Roseanne
Join our Study Group when certified

[This message has been edited by Roseanne Zhang (edited August 25, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Interface as a parameter