• Post Reply Bookmark Topic Watch Topic
  • New Topic

Child class calls method in Parent class.  RSS feed

 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!
Below I have two classes. a Parent class and a Child class. The Parent creates the Child class and the Child calls a method in the Parent class.

It works, but I'm confused on how it works or if it is the correct way to do this.

Any help or links to resources that can explain how this works of the proper way to go about this concept would be very helpful.

Parent.java


Child.java

[ September 16, 2005: Message edited by: Stephen Boston ]
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor relationship is a red herring. popup() is static, so it's possible to call the method without an object reference.
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is that an acceptable programming practice?

The reason I ask is that I'd like to use something like that in a class to pass some information back to the parent class.

What I've done before when I needed to do that is to create get methods in the child class and have the parent class check on child get method (isReady) to get a boolean return. If the return was true, then that parent will get the second get method (returnedValue) to get the value to finish up a process. I would use a timer to cause the parent process to 'wait' for the child process to complete and signal ready. That just seemed 'hackish' to me.

I'm just trying to find the correct way to do this.

AND! Thanks for the reply!
[ September 16, 2005: Message edited by: Stephen Boston ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that with statics, you are dealing with two separate conceptual objects -- the "Parent class" and the "Child class". If you create an instance of the Child class, then you have one instance that is a Child and is a Parent -- it's both.

So, if you had:



You've now created a "hook" -- a place in the parent where subclasses can adjust the value as they see fit. (You could even make the method abstract and force subclasses to implement it. Certainly in this case that might make sense, as the method as it is empty.)

But what you are talking about is two separate objects -- not a Parent-Child isa relationship. And your solution is not necessarily a bad one: a loop of:



is acceptable in a multi-threaded environment, and:



could be used in a single-threaded environment. Of course, Threads bumps this out of the beginner's forum, so I woln't talk about them here.
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I'll try to keep it here in Beginners (as that is what I am). But would this be a more preferred way to achieving it?

Parent.java


Child.java
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be some confusion here in naming your classes Parent and Child because neither of them extends the other. So if you are thinking of this in terms of inheritence, there is no relationship between these two classes despite their names, unless, of course, there is a typo in the code from the OP.

Layne
 
Stephen Boston
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I understand that now.
I had just used the classes I made up for testing modified for the second code posting. Just lazyness on my part I guess.

Diving into understanding threads.... my head hurts.

BTW; Thanks to all helped me out here!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!