Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

question about polymorphism  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two classes, a Parent, and a Child. Parent has two methods, one of which is overloaded by Child.



Okay, this is very simplified. Here's my question. If later in the code, Child.initializeGUI is called, will initializeGUI call the Parent getPanel method or the Child getPanel method? I think (and hope) it's the latter, but I wanted to verify.
[ April 23, 2008: Message edited by: Jess Dancel ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just give it a try, maybe identifying which of the getPanel() methods the panel comes from, eg by setting it's color before returning it? This is the best way of learning how things work ...
 
Jess Dancel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good idea, Guido. I'm currently knee-deep in the actual code that this version represents, but I'll try compiling a simple program when I get a chance.

I am, however, interested in the academic answer as well.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Academically, the getPanel() method of the Child class should be the one to be invoked. The only way of accessing Parent.getPanel() on a Child object is from within the Child object using super.getPanel().

Hope that helps.
 
Jess Dancel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that does help.
 
Yes, my master! Here is the tiny ad you asked for:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!