[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

Constructor injection

I was reading folowing lines and I did not understand clearly.

If a constructor takes two,more parameters of the same type, it may be
difficult to determine what each parameters purpose is.

Constructor injection does not lend itself readily to inheritance. A beans
constructor will have to pass parameters to super() in order to set private
properties in the parent object.

Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
0
gudii9
Asked:
gudii9
  • 2
1 Solution
 
cmalakarCommented:
Look at this..

http://drag0sd0g.blogspot.com/2010/11/spring-framework-constructor-vs-setter.html

Basically a debate on use of constructors and setters.

>>If a constructor takes two,more parameters of the same type, it may be
difficult to determine what each parameters purpose is.


This statement was written with respect to setter method. For example When creating an object of type Employee, say there is a constructor which takes two string arguments (String firstName, String lastName), when you create an Employee like

new Employee("john", "smith") 

Open in new window


in the above line, is not so clear that "john" is first name and "smith" is second name. Hence it may be cause confusion.

If the same thing is replaced with setter method it will be like the following.
Employee e = new Employee();
e.setFirstName("john");
e.setSecondName("smith");

Open in new window

And setter method is very clear, while looking at the code.
0
 
cmalakarCommented:
>> Constructor injection does not lend itself readily to inheritance. A beans
>> constructor will have to pass parameters to super() in order to set private
>> properties in the parent object.

If there is any parent class, then while creation, it is very obvious that we should call super() with the required parameter.
On the other hand, setXXXX methods works without need of any super()
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now