Results 1 to 6 of 6
- 09-28-2009, 10:50 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
Child-Class Calling a Method in a Parent-Class
Is there anyway when a class creates another class, that the class it has made (The Child-class) can call a method in the class that made it, e.g:
I have a class called Parent, it creates a class called Child. I want Child-class Child, to call the method ThisMethod that belongs to the Parent-class Parent.
I've tried methods like extending the class and using super but it seems to create the parent class again and I lose all my varibles. So is there and simple way just to call a method in the parent class I'm missing here?
If you don't understand what I mean, I can write up some example code to clarify it more.
,Thanks
- 09-28-2009, 11:18 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Pass the "child" an instance of the "parent" in the constructor
Java Code:// test/Parent.java package test; public class Parent { public void printA() { System.out.println("a"); } public Child createChild() { return new Child(this); } } // test/Child.java package test; public class Child { private final Parent parent; Child(Parent parent) { this.parent = parent; } public void printParent() { parent.printA(); } } // test/Test.java package test; public class Test { public static void main(String[] args) { Parent p = new Parent(); Child c = p.createChild(); c.printParent(); } }
- 09-28-2009, 11:29 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
Thanks, this looks to be very helpful, will try it out.
Also thanks for the speedy resounce.
- 09-28-2009, 01:28 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
Sorry this did not seem to work for what I wanted, heres a code example of what I want to happen:
Forgot to mention I was using threads before, not sure if it was too important in the end result.Java Code:// test/Test.java package test; public class Test { public static void main(String[] args) { Parent p = new Parent(); p.start(); } } // test/Parent.java package test; public class Parent extends Thread{ public int CHILD_CLASS = 50; Child[] c = new Child[CHILD_CLASS]; public void run() { int i = 0; while (CHILD_CLASS > i) { c[i] = new Child(); c[i].start(); i++; } } public void cMessage(int num){ System.out.println(num); } } // test/Child.java package test; public class Child extends Thread{ public void run() { //Use the parent method cMessage() to send a number back } }
EDIT
I seem to of found a solution after all using the idea behind the code from masijade
being put into the child class andJava Code:private final Parent parent; Child(Parent parent) { this.parent = parent; }
being put in the parent class when the child is created using the above constructor.Java Code:c[i] = new Child(this);
I can then call a method in the class parent from child using
For anyone wondering my solution.Java Code:parent.cMessage(23);
Last edited by Blah_; 09-28-2009 at 01:53 PM.
- 09-28-2009, 07:16 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
I'm sorry, but that is what you were meant to do. The code posted was just an example, not meant to be a simple cut-n-paste solution.
- 09-29-2009, 02:48 AM #6
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Calling a class method from another class
By caro in forum New To JavaReplies: 4Last Post: 06-10-2009, 01:12 AM -
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling method from another class
By asahli in forum New To JavaReplies: 1Last Post: 12-15-2007, 06:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks