This week's book giveaway is in the JavaScript forum.
We're giving away four copies of JavaScript Promises Essentials and have Rami Sarieddine on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Is Polymorphic Bean reference in conformity with Java polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is Polymorphic Bean reference in conformity with Java polymorphism" Watch "Is Polymorphic Bean reference in conformity with Java polymorphism" New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Polymorphic Bean reference in conformity with Java polymorphism