File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Returning an interface from a Factory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Returning an interface from a Factory" Watch "Returning an interface from a Factory" New topic
Author

Returning an interface from a Factory

Barry Brashear
Ranch Hand

Joined: Jun 05, 2001
Posts: 303
Is it possible to return an instantiated interface from a Factory? If
so could someone include an example of this?

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Since you cannot create an instance of an interface, no. But you can easily create and return an object that implements that interface.

You can also do so in an anonymous manner. For example:



Now some might say that you are instantiating the interface with this code, but really you are creating an instance of an anonymous class that implements the interface.
[ November 09, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jon Parise
Ranch Hand

Joined: Jul 03, 2007
Posts: 81
By definition you cannot instantiate an interface directly, you can only instantiate it's subclasses.

The closest you can come is to instantiate one of it's subclasses. This is great for things like the factory method.

So said I had an interface called Interface1 and then I had 2 classes that implemented it called Class1 and Class2.


In my factory I could have a Create method that appears to return an instance of the interface, but truly returns an instance of it's child which is through polymorphism also an instance of Interface;


public interface Interface1{

}

public Class1 implements Interface1{
public Class1(){}
}

public Class2 implements Interface2{
public Class2(){}
}



public class Factory{

public Factory(){}

public Interface create(int option){
if(option ==1){
return new Class1();
}
else{
return new Class2();
}
}


}


You can play with instanceof to see which Class is returned by the factory. This adds a level of information hiding to your design. Based on conditions, the factory returns the class that is best suited. The user only sees the interface though and doesn't need to know if they have a class 1 or class2.

This also adds some extensibility, since with this method if a new condition arises requiring Class3, you can simply add it to the factory and leave everything else intact.

I hope that helped,

Jon
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Originally posted by Jon Parise:
By definition you cannot instantiate an interface directly, you can only instantiate it's subclasses.

Interfaces do not have sub-classes, they have implementers. Sometimes, semantics are important.
Jon Parise
Ranch Hand

Joined: Jul 03, 2007
Posts: 81
Good point, bad terminology use on my part.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Returning an interface from a Factory