• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Polymorphic Bean reference in conformity with Java polymorphism

 
Rajat Sharmanaive
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear

Apologies to you and to those who were inconvenienced in any way by this careless gesture. Din't pay attention.

Rajat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic