• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance in Spring

 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the below code,

I have two classes Person and Student, such a way that Student extends Person.

Person class


Student class


and my configuration file is as follows


and now through my client, I'm expecting to instantiate Student bean through IoC and need to display the id, name and class. But unfortunately, I'm able to display only the class name.




From the output, I observe that the constructor of Person is invoked when IoC instantiates Student as expected. But when I try displaying the ID and Name, they are displayed as null. Doesn't Spring container assign the values for parent object, before setting them to child object.

 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Schandha Ravi wrote:From the output, I observe that the constructor of Person is invoked when IoC instantiates Student as expected. But when I try displaying the ID and Name, they are displayed as null. Doesn't Spring container assign the values for parent object, before setting them to child object.

Spring doesn't know anything unless you tell it. That is those are just two beans spring instantiates. I haven't tried it but check this
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Vijitha Kumara.
 
Tomas Bilek
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must fill attribute "parent". So for example parent="Person".

Good Luck !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic