Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Returning an interface from a Factory  RSS feed

 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to return an instantiated interface from a Factory? If
so could someone include an example of this?

Thanks.
 
Marshal
Posts: 67200
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 67200
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point, bad terminology use on my part.
 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!