aspose file tools
The moose likes Beginning Java and the fly likes Child class calls method in Parent class. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Child class calls method in Parent class." Watch "Child class calls method in Parent class." New topic
Author

Child class calls method in Parent class.

Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
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 ]

Steve<br /> <br />No matter where you go, there you are.<br /> <br />"My evil self is at the door, and I have no power to stop it."
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

The constructor relationship is a red herring. popup() is static, so it's possible to call the method without an object reference.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
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

Joined: Aug 20, 2001
Posts: 1815
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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
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!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Child class calls method in Parent class.
 
Similar Threads
Static binding Vs Dynamic Binding
Polymorphism applies to instance methods not instance variables?
same method inheritance
Inheritance - weird behaviour ?
override or overload?