Results 1 to 6 of 6
  1. #1
    Blah_ is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Question 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

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    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();
        }
    }

  3. #3
    Blah_ is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    Thanks, this looks to be very helpful, will try it out.
    Also thanks for the speedy resounce.

  4. #4
    Blah_ is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    Sorry this did not seem to work for what I wanted, heres a code example of what I want to happen:

    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
      }
      
    }
    Forgot to mention I was using threads before, not sure if it was too important in the end result.


    EDIT

    I seem to of found a solution after all using the idea behind the code from masijade

    Java Code:
      private final Parent parent;
      
      Child(Parent parent) { 
        this.parent = parent; 
      }
    being put into the child class and

    Java Code:
    c[i] = new Child(this);
    being put in the parent class when the child is created using the above constructor.

    I can then call a method in the class parent from child using

    Java Code:
    parent.cMessage(23);
    For anyone wondering my solution.
    Last edited by Blah_; 09-28-2009 at 01:53 PM.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    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.

  6. #6
    Blah_ is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    Sorry, was not too sure at first. as the example had the main thread create both objects. Still thanks alot for the help.

Similar Threads

  1. Calling a class method from another class
    By caro in forum New To Java
    Replies: 4
    Last Post: 06-10-2009, 01:12 AM
  2. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  3. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  4. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  5. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •