0

For clarity consider following design -

Interface MyIface{
 myInterfaceContract(someObject);
};

Class Child1 implements MyIface{
   myInterfaceContract(someObject){ //implemented }
   myChild1Action(){ //implemented }
}

Class Child2 implements MyIface{
   myInterfaceContract(someObject){ //implemented }
   myChild2Action(){ //implemented }
}

I know how to achieve interface injection through spring annotation. doubt is - If interface based injection is done then how can I access methods directly implemented in my child class i.e. myChild1Action()/myChild2Action()

Am i doing or thinking drastically wrong?

2
  • 1
    You can use instanceof and cast to the appropriate implementation (Child1 or Child2). The fact you're trying to do it probably means your design isn't ideal though (you should always try and code to an interface)... Commented Oct 18, 2013 at 9:23
  • If my child1 and child2 requires some specific implementation then what can be done for e.g. interface offers create/update/delete but my child1 require say some finder along with C/U/D and child2 requires some other function. what could be done in this case? Commented Oct 18, 2013 at 9:32

1 Answer 1

1

I'd say your thinking is confused. If you are injecting based on the interface then that implies that the interface gives you all the functionality you need. If you need methods from the implementations then that interface isn't giving you what you need and injecting a concrete implementation is what you need to do.

Most of the time, if you are using instanceof to find what class you have so you can call some extra method, then it's a sign your design is broken.

You might find it helpful to define a "Finder" interface and have Child1 implement that as well and inject the bean into a Finder field as well as a MyIFace field.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.