Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Calling a parent method

Options
  • 11-12-2003 1:31pm
    #1
    Closed Accounts Posts: 999 ✭✭✭


    Hi guys,
    I got a bit of a problem.
    I'm writing a program in which I have three classes, run, processorand simGui.
    When I execute the run class it creates an object of the processor class called mc6809

    processor mc6809 = new processor();

    Within the processor class I create a GUI using the simGui class,

    simGui gui = new simGui();

    Okay, simple so far.
    But in the simGui class I have an if statement like so,
    if(e.getActionCommand().compareTo("Get Program")==0)
    	{
    		String program = mc6809.getProgram();
    		System.out.println("\n\n" + program + "\n\n");
    	}
    

    As you can see I'm trying to call the getProgram() method of the processor class but I'm getting a cannot resolve symbol error for the mc6809 object from the child simGui class.
    So I'm obviously doing something wrong but, being a bit uneducated in proper java syntax etc., I'm a bit lost.
    Can anyone show me the light?


Comments

  • Registered Users Posts: 841 ✭✭✭Dr Pepper


    Hi Raz,

    Not too familiar with the parent-child relationships myself (even though it's the backbone of Java!). I think if you call the parent by the class name (processor) instead of the name of the instance (mc6809), it might work:

    i.e. String program = processor.getProgram();

    Also, you could try calling the instance mc6809 from the 'grandparent' class (run) where it is declared:

    i.e. String program = run.mc6809.getProgram();

    Not entirely sure but sometimes the blind have to lead the blind!!!


  • Registered Users Posts: 4,666 ✭✭✭Imposter


    Are you sure the class is 'processor'. The first letter of classes should really be capitalised.

    If the classname is processor then maybe your getProgram() is private.
    If it is being called from a different class it needs to be public (unless the calling class is an extended class, in whioch case protected is minimum neccesary).

    Does your code compile properly? Post your 'processor' class if none of the above work.


  • Closed Accounts Posts: 857 ✭✭✭davros


    I think this is what you are asking. Without changing the structure of your code at all...

    you can declare a constructor for simGui that takes a processor object:

    private processor p;

    simGui(processor proc) {
    this.p = proc;
    }

    Then, when you create your simGui object, you can pass in the processor object:

    simGui gui = new simGui(this);

    And inside simGui:

    String program = p.getProgram();


  • Closed Accounts Posts: 999 ✭✭✭Raz


    Thanks Pepper but no cigar unfortunately.
    I get the same cannot resolve symbol error when calling from the grandparent class and I get this,
    non-static method getProgram() cannot be referenced from a static context
                 String program = processor.getProgram();
                                           ^
    
    when I use the class itself. But then maybe you can help with that one? :)


  • Registered Users Posts: 4,666 ✭✭✭Imposter


    getProgram is static.

    It needs to not be static.

    public String getProgram(){
    //stuff
    }

    or you could call:
    processor.getProgram() and it would work but you wouldn't have your instance mc6809.


  • Advertisement
  • Closed Accounts Posts: 999 ✭✭✭Raz


    The class is called processor and it is public.
    I'm going to attempt davros' solution.
    As a bit of info about the program,
    I'm writing a simulator which will run assembly code for the Motorola mc6809 processor.
    The processor class I'm writing will be the core of the program, implementing the instruction set for the mc6809 and creating objects of all the child classes. (gui, registers, memory etc.)

    davros, you wrote,

    "you can declare a constructor for simGui that takes a processor object:"

    I have to say this confused me a little. If the simGui takes a processor object does that make the simGui kinda more important?
    I'm not sure if that'll make sense to you, cos of my own confusion.

    Anyway, back to business.
    The processor class
    
    	/**
    	processor.java created by Rory Clerkin, DCU.
    	
    	This is the processor of the MC6809 simulator, created as part of my 
    	Electronic Engineering final year project.
    	It will call the GUI and all other classes.
    	
    	*/
    
    
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    import java.util.*;
    
    public class processor
    {	
    	
    	simGui gui = new simGui();
    
    	public processor()
    	{
    
            	gui.addWindowListener(new WindowAdapter()
    		{
           		    	public void windowClosing(WindowEvent e)
    			{
    		               	System.exit(0);
           			}
           		});
    	
    		gui.pack();
    		gui.setVisible(true);
    	}
    
    
    	public void getProgram()
    	{
    		String program = this.gui.getProgram();
    		return program;
    	}
    	
    

    I've imported all those packages because I will most likely need them as the project progresses.


  • Closed Accounts Posts: 999 ✭✭✭Raz


    getProgram is static.

    It needs to not be static.

    public String getProgram(){
    //stuff
    }

    or you could call:
    processor.getProgram() and it would work but you wouldn't have your instance mc6809.


    Awwww nuts. I didn't notice that. Thanks imposter. I'll see if that works.
    Is that all that static means? That the method isn't returning anything?


  • Closed Accounts Posts: 999 ✭✭✭Raz


    Well that helped some, but I'm still getting this errors,
    .\simGui.java:217: non-static method getProgram() cannot be referenced from a static context
                                    String program = processor.getProgram();
    

    which refers to these lines in simGui,
    if(e.getActionCommand().compareTo("Get Program")==0)
    	{
    		String program = processor.getProgram();
    		System.out.println("\n\n" + program + "\n\n");
    	}
    

    The getProgram() method of processor is returning a String now but I guess I'm back to the original question of parent child interaction


  • Registered Users Posts: 4,666 ✭✭✭Imposter


    The method declaration of the method that calls getProgram is static.
    Go read up on static methods and understand when, where and how they are used.

    (I'm not trying to be smart here, I just think that your understanding of static methods is the problem)


  • Closed Accounts Posts: 999 ✭✭✭Raz


    I have to agree Imposter, it is a bit of a problem. I'm not exactly new at Java but my education in it wasn't that thorough. But I do like programming though so I'll get it eventually :)
    Originally posted by davros
    you can declare a constructor for simGui that takes a processor object:

    private processor p;

    simGui(processor proc) {
    this.p = proc;
    }

    Then, when you create your simGui object, you can pass in the processor object:

    simGui gui = new simGui(this);

    And inside simGui:

    String program = p.getProgram();
    The line above above, "private processor p;" would you declare that in the processor class or the simGui? I'm finding it difficult to make sense of those few lines.
    If I declare it in the processor then it won't be the same instance as the mc6809 object I created in the "run" class bu I can see it would work.
    Or would it be the same instance as when you construct the simGui you use "this".
    So does that mean it uses the mc6809 object which is just created or does it just use the processor class in general?
    I hope that makes a bit of sense :)


  • Advertisement
  • Closed Accounts Posts: 999 ✭✭✭Raz


    Well I implemented davros' suggestion and it worked quite nicely. After writing the last post I think I helped clarify how it worked in my own mind and I'm pretty sure of how it works.
    For now, I can continue with developement. Thanks for your help guys. You've been instrumental :)


  • Closed Accounts Posts: 857 ✭✭✭davros


    [EDIT]Oops - too late :) [/EDIT]
    Originally posted by Raz
    The line above above, "private processor p;" would you declare that in the processor class or the simGui?
    That belongs in simGui.

    Let me see if I can explain it better. You want your simGui instance to know which processor instance it is inside.

    So, when you create your simGui instance [simGui = new simGui(this);] you pass it a reference to the current class (i.e. mc6809). 'this' is a Java keyword meaning 'the current class.

    Since the current class is a processor, you have to declare a simGui constructor that can take a processor:

    simGui(processor proc) {
    }

    You have to store that processor somewhere, so you declare a processor variable inside the simGui class:

    private processor p;

    So the whole simGui class might look something like this:

    public class simGui
    {

    private processor p;

    /**
    * Constructor
    */
    public simGui(processor proc)
    {
    this.p = proc;
    }

    public void someMethod() {
    ...
    if(e.getActionCommand().compareTo("Get Program")==0)
    {
    String program = p.getProgram();
    System.out.println("\n\n" + program + "\n\n");
    }
    ...
    }

    }


  • Closed Accounts Posts: 999 ✭✭✭Raz


    Originally posted by davros
    [EDIT]Oops - too late :) [/EDIT]
    Not at all. It's helped me understand it. Thanks for your help davros.


  • Registered Users Posts: 841 ✭✭✭Dr Pepper


    Oops - too late also

    I need to get to grips with the whole 'static' thing myself. It often appears as a thorn in my side! BTW
    public void getProgram()

    How does this return a String?

    Is it not supposed to be:

    public String getProgram()
    ?


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Just some general waffle which may help clear things up.

    First of all, no object can "automagically" know what its parent is. Why? Well - who says an object has only one parent? You can have the same widget which is a "child" of half a dozen widgetCollection objects. Which is its parent?

    So, any "call to parent" relationship must be manually implemented.

    The code which Davros supplied is - IMHO - the right way to do it....getting the parent to pass a reference to itself to the child when it constructs it. Now the child has a reference back to the parent which is can use.

    Statics could also be used, but they suffer sime limitations (w.g. a static method cannot reference any non-static methods or variables). It should also be remembered that static information is shared across all objects of the class type.

    To explain : lets say I have a class Widget. It contains a public static string named TestString. No matter how many widget objects I create, they all share the one copy of TestString!!

    So, if I were to do this :

    private widget test1 = new widget();
    private widget test2 = new widget();

    test1.TestString = "Setting into Test1";
    test2.TestString = "Setting into Test2";

    System.out.println(test1.TestString);


    What gets printed?
    If TestString were a normally defined variable, then each object would have its own copy, and tehrefore I would get back "Setting into Test1".

    But I said I defined it as a static. That means that there's only one "storage bin" allocated for that value for all objects. So, I initially wrote "Setting into Test1" into this bin. Then I wrote "Setting into Test2" into it, and then I'm reading the current value back in the println.

    So I get as my output : "Setting into Test2".

    This, incidentally, would be a really dumb reason to use statics....but it might help explain a little how they work.

    Incidentally, I could also have done :

    System.out.println(widget.TestString);

    Unlike regular methods and properties, statics can (and should be) referenced against the class name.

    jc


  • Registered Users Posts: 841 ✭✭✭Dr Pepper


    Cheers bonkey,

    That does clarify a lot - for me anyway!!


  • Closed Accounts Posts: 999 ✭✭✭Raz


    Definitely helpful, but I still need to look into it more.


Advertisement