I am consuming a webservice using my java web application.This returns a number of data among which there is a attribute named Consumer. This is represented as an object when the WSDL is converted to Java code. There is only one string variable id inside this class. There is another class Person which extends Consumer. This has a number fields like firstName , lastName etc. In the JSF code , there is a reference to consumer.firstName and the value pertaining to firstName is being printed properly when person object is returned as part of response xml. This confuses me a bit because consumer object does not contain firstName and still the value is getting printed properly when consumer.firstName is used. Please can you help me in understanding this.
JSF Code
<h:outputText value="#{myBean.consumer.firstName}" />
Backing Bean
public Consumer getConsumer() {
return consumer;
}
Consumer Class
public class Consumer implements java.io.Serializable {
private java.lang.String id;
public java.lang.String getId() {
return this.id;
}
public void setId(java.lang.String id) {
this.id = id;
}
}
Person Class
public class Person extends Consumer {
private String firstName;
private String lastName;
private String dateOfBirth;
// Getters and Setters
}
JSF uses reflection to call the methods on your objects. It doesn’t know what the type of
myBean.consumeris. All it knows is that you asked thefirstNameof this object. So, what it does isSo, even if the method
getConsumer()declares that it returns aConsumer, if, at runtime, the actual object returned bygetConsumer()is aPerson, JSF will search for agetFirstName()method inPerson, it will find it, and will happily call it and outut its result.