• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Reflection: Instantiate Interface Implementation

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say I have the following:





Ok, so what I need to do is the following: Using reflection, I need to instantiate ThatInterface with an instance of ThisAndThat class. I can instantiate ThisAndThat with no problem.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Lowell:
Say I have the following:





Ok, so what I need to do is the following: Using reflection, I need to instantiate ThatInterface with an instance of ThisAndThat class. I can instantiate ThisAndThat with no problem.



Henry,

You solved your own problem in the text of your post. just take the code that instantiates ThisAndThat and move it into a method that returns ThatInterface. You're probably confused in thinking you need to instantiate ThatInterface which is not possible. The whole idea is to have the caller use the ThatInterface as the type and then the code you have to instantiate ThisAndThat can be moved into a factory method that returns ThisInterface.

In my above example all you would need to do is inject TheOtherThing with ThatFactory using an assembler like Spring or just plain ol' wonderful Java.

(Man I love the way that codes out! Good example Henry!)
 
Henry Lowell
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I find of figured that out just before I got your response. Everything works great. Thanks again!
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic