0

I have an interface or abstract class and I have another class which inherits the interface or abstract class and I overridden the method of of interface or abstract class. Now i want to Initialize the interface or abstract class type variable by the object of the concrete class which inherits the interface or abstract class through XML in spring. Is there any way to do it.Please suggest me. Thanks in advance.

DaveH
  • 7,187
  • 5
  • 32
  • 53
Altaf Khan
  • 17
  • 4

1 Answers1

0

If you have a class implementing an interface, you have to instantiate directly the concrete class that implements the interface. So if you have:

public interface MyInterface{
}

public class MyClass implements MyInterface
}

In your applicationContext.xml, you'll put:

<beans>
   <bean id="myBean" class="org.my.package.MyClass">
</bean>

Remember that you can not instantiate an interface, so it only makes sense to create a bean from the concrete class.

In case that you have a parent class, say MyParent, and you want a class to extend from it, you can do:

<bean id="myBean" parent="MyParent">
</bean>

Hope it helps a bit!

Ps: To learn more about the difference between inherits and implements, you cand read this: Implements vs extends: When to use? What's the difference? and this Interface vs Abstract Class (general OO)

EDIT: snippet of XML inheritance configuration.

Community
  • 1
  • 1
Rocío García Luque
  • 3,597
  • 31
  • 31