Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem understanding overriding.

Status
Not open for further replies.

justride

Programmer
Jan 9, 2004
251
US
I thought I understood when a subclass method could override that of its parent class method if the signature's were the same, however I have just seen an example of the opposite and I am freaked out.

example:
Code:
parentclass (){
somemethod(){
reply = processRequest( request ){
}
protected processRequest( E_Message request ){
}
}
...

subclass extends parentclass(){
protected processRequest( E_Message request ){
}
}
reply calls processRequest from it's subclass, how can this be, how does it know about the method in its subclass? why isnt it calling the processrequest in its own class. I could understand if it was a subclass but its a parent class. also, if this is normal what if there were other subclasses that had the same method signature, how the heck would the parent class know which subclass method to call????

Thanks
 
It will depend on which class are you calling the method on.

If you call it on an instance of the parent class, the parent processRequest will be called. If you do the same on the child class, the child's one will do.

Cheers,
Dian
 
Yes, thats the problem I was having. THeres a ton of threads in this app and I didnt relaize I was in the child instance until I saw it on the stack as (this). THanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top