[solved]Need to Change Value in Parent class.

Okay so I have a text field and a GLCanvas which is in my “Parent” class.

In my “game” in the GLCanvas I want to be able to alter the text field in the “Parent” class.

I know I could make a text file and read it or whatever, but that is reallly sloppy, and not Applet friendly. I’m sure this is more of a basic programming question than anything else.

I’m gonna see if there isn’t a way I can have the text feild created and handled in the sub-class…

Or do I need to make an Abstract class? blah the lame things I’ve yet to be taught in CIT130…

Nevermind I figured it out.

I just had to get pass the parent class into my subclasses constructor, save it globally to the subclass then call it. Woo!

public class Parent {
    private TextField textField = new TextField();

    public TextField getTextField()
    {
        return textField;
    }
}

public class Child extends Parent {

    public Child()
    {
        getTextField().setText("some text");  //modifies the parent's text field
    }
}

Mine looked more like this but thank you for your response! :slight_smile:


public class parent {
     private String text = "OLD TEXT"; 

     public void changeText(String newText){
           text = newText;
     }
}



public class child{
	//TOP
	private parent parentTOP;
	
	public child(parent par) { 
	    parentTOP = par;
	} 

       public somethingHappened(){
            parentTOP.changeText("NEW TEXT");
       }
}

Hey whatever works for you :wink:

Btw is your “child” class a subclass of your “parent” class?
(By that I mean: public class Child extends Parent)

If so, there is no need to pass the parent class as a parameter in your Child constructor.
You can just do something like:

public somethingHappened() {
            changeText("NEW TEXT");
}

since the Child class has access to that method in the Parent class.

nope, no inheritance. I worded that poorly.

I used to code in VB and VB .net and they use MDIParents and MDIChilds those are “forms” that are completely unrelated(but may coincide within one another). My lack of Java vocab led me to say something confusing and not what I wanted, as always haha.

But once again thanks. ;D