| Author |
Is Polymorphic Bean reference in conformity with Java polymorphism
|
Rajat Sharmanaive
Ranch Hand
Joined: Jun 30, 2011
Posts: 41
|
|
Hi
I am new to J2EE. Learning through HFSJ by K&B. Following is my query from the same.
The jsp standard action:
In the generated servlet, translates to:
Following are my 2 beans:
But according to the book (which is indeed true, as this is what happens). The following action sets all the properties.
From what i have learnt w.r.t to Java Polymorphism is that a reference variable can invoke (at runtime, through dynamic dispatch) only the methods that it knows about. This means (assume B is subclass of A)
a can invoke methods that are overridden.
a cannot invoke:
methods that are only in B // This is what seems to happen here
methods that are only in A //off course, because the actual object instantiated is of type B.
How then, is personBean able to set empId which is not overridden and which solely belongs to the subclass. Please help. It is not a very encouraging feeling to have when you are thinking about giving SCJP and find your Inheritance fundamentals challenged. Thanks.
|
Rajat Sharma
|
 |
bhanu chowdary
Ranch Hand
Joined: Mar 09, 2010
Posts: 256
|
|
a cannot invoke:
methods that are only in A //off course, because the actual object instantiated is of type B.
When you are extending a class the subclass inherits all the methods of the super class. The below code prints abcd.
|
 |
Rajat Sharmanaive
Ranch Hand
Joined: Jun 30, 2011
Posts: 41
|
|
Hi Bhanu,
I stand corrected. Thanks for pointing that out. That was very basic. Somehow, i lost it. The main doubt however, still remains unresolved.
Hoping that also does not get solved in a way, that exposes my "expertise" . Looking forward to help.
|
 |
Rajat Sharmanaive
Ranch Hand
Joined: Jun 30, 2011
Posts: 41
|
|
Hi Bear
Apologies to you and to those who were inconvenienced in any way by this careless gesture. Din't pay attention.
Rajat
|
 |
 |
|
|
subject: Is Polymorphic Bean reference in conformity with Java polymorphism
|
|
|