Calling father from the child class?

Hi...

And I want to learn how to:

public class FATHER{

int SAMPLE=0;

FATHER(){

Child mychild=new Child();

}

}

class Child{

Child(){

// I am child and I want to set my father's(who created me) SAMPLE var...

}

}

here is a small example.. I could not reached fathers variables in the child.. there must be an easy way to do that ..

Thanks ...

[438 byte] By [serdarra] at [2007-11-26 19:11:20]
# 1
Make the Child class extend the Father class.Message was edited by: Djaunl
Djaunla at 2007-7-9 21:08:25 > top of Java-index,Java Essentials,New To Java...
# 2
pass the father to the child when you make a child.
mkoryaka at 2007-7-9 21:08:25 > top of Java-index,Java Essentials,New To Java...
# 3
Look at this too: http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html
Djaunla at 2007-7-9 21:08:25 > top of Java-index,Java Essentials,New To Java...
# 4
> Make the Child class extend the Father class.Sure, do that and wait for an OutOfMemoryError or a StackOverFlowErrorwhichever comes first ;-)kind regards,Jos
JosAHa at 2007-7-9 21:08:25 > top of Java-index,Java Essentials,New To Java...
# 5

> > Make the Child class extend the Father class.

>

> Sure, do that and wait for an OutOfMemoryError or a

> StackOverFlowError

> whichever comes first ;-)

>

> kind regards,

>

> Jos

Exactly!

Actually, why would that happen?

Djaunla at 2007-7-9 21:08:25 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks to every one.. but I still cant solve the problem :( some of my classes ALREADY extends (Jframe or Jpanels for new windows etc).. but new window classes some times has to call the class in which he had been created...
serdarra at 2007-7-9 21:08:26 > top of Java-index,Java Essentials,New To Java...
# 7
> Actually, why would that happen?Because the father tries to create a child, it will become recursive.Kaj
kajbja at 2007-7-9 21:08:26 > top of Java-index,Java Essentials,New To Java...
# 8

> Thanks to every one.. but I still cant solve the

> problem :(

>

> some of my classes ALREADY extends (Jframe or Jpanels

> for new windows etc).. but new window classes some

> times has to call the class in which he had been

> created...

See reply #2

kajbja at 2007-7-9 21:08:26 > top of Java-index,Java Essentials,New To Java...
# 9
Pass as an object?... I have tried but failed..pass as an object as (this) from father and catch it in the constructors(of the child) arg.. is that what I must try to do?thanks again
serdarra at 2007-7-9 21:08:26 > top of Java-index,Java Essentials,New To Java...
# 10

I have a window.

in it a Jpanel.. . (a class that extends Jpanel)

and in Jpanel some row Jpanels (all as a sub class)

when a row clicked, the other rows must know that theN th row is active now..

so that simply I have planned to put an integer inthe Jpanel so rowJpanels set and read it..

serdarra at 2007-7-9 21:08:26 > top of Java-index,Java Essentials,New To Java...
# 11
> > Actually, why would that happen?> > Because the father tries to create a child, it will> become recursive.> > KajOh, so it will become recursive due to the constructor in the Father class?
Djaunla at 2007-7-9 21:08:26 > top of Java-index,Java Essentials,New To Java...
# 12

> > > Actually, why would that happen?

> >

> > Because the father tries to create a child, it

> will

> > become recursive.

> >

> > Kaj

>

> Oh, so it will become recursive due to the

> constructor in the Father class?

Nope, because of the ctor of the child class because it'll call either

implicitly or explicitly the father's ctor again.

kind regards,

Jos

ps. see for yourself:class Father {

private class Child child;

public Father() {

System.out.println("father ctor");

child= new Child();

}

}

public class Child extends Father {

public Child() {

System.out.println("child ctor");

}

}

JosAHa at 2007-7-9 21:08:26 > top of Java-index,Java Essentials,New To Java...
# 13

> Nope, because of the ctor of the child class because

> it'll call either

> implicitly or explicitly the father's ctor again.

>

> kind regards,

>

> Jos

>

> ps. see for yourself:> class Father {

>private class Child child;

> public Father() {

>System.out.println("father ctor");

> child= new Child();

>}

> public class Child extends Father {

>public Child() {

>System.out.println("child ctor");

> }

> }

So basically because Child extends Father, whenever Child's constructor is run, Father's constructor is run, which then runs Child's constructor, etc?

Djaunla at 2007-7-9 21:08:26 > top of Java-index,Java Essentials,New To Java...
# 14

> So basically because Child extends Father, whenever

> Child's constructor is run, Father's constructor is

> run, which then runs Child's constructor, etc?

Yup, you've got it: whenever a derived class's ctor is run, it first runs the

base class's ctor but in this particular case the base class's ctor

explicitly creates a new derived class again: *whammo* it's turtles all

the way down.

kind regards,

Jos

JosAHa at 2007-7-9 21:08:26 > top of Java-index,Java Essentials,New To Java...
# 15

> > So basically because Child extends Father,

> whenever

> > Child's constructor is run, Father's constructor

> is

> > run, which then runs Child's constructor, etc?

>

> Yup, you've got it: whenever a derived class's ctor

> is run, it first runs the

> base class's ctor but in this particular case the

> base class's ctor

> explicitly creates a new derived class again:

> *whammo* it's turtles all

> the way down.

>

> kind regards,

>

> Jos

That's very useful to know; thanks a lot.

Djaunla at 2007-7-9 21:08:27 > top of Java-index,Java Essentials,New To Java...
# 16

As the second reply tells..

I have solved the problem by sending the fathers it self as a reference... Maybe there are some beginners as me, for further readers I have noted it here:

so basicly:::

public class FATHER{

String Childs_saying;

FATHER(){

Child mychild=new Child(this);

}

}

class Child{

Child(Father dearfather ){

dearfather.Childs_saying="I am talking to you father:)";

}

}

that way, could be used on functions of father too. A good way I think.. That is some thing that not only the father knows his child, also child nows his father :)

Thanks All

serdarra at 2007-7-9 21:08:28 > top of Java-index,Java Essentials,New To Java...
# 17
> A good way I think.. That is some thing that not only> the father knows his child, also child nows his> father :)I don't think that's a good way. Superclasses should not depend on specific subclass implementations.~
yawmarka at 2007-7-9 21:08:28 > top of Java-index,Java Essentials,New To Java...
# 18
> I don't think that's a good way. Superclasses should> not depend on specific subclass implementations.++
jverda at 2007-7-9 21:08:28 > top of Java-index,Java Essentials,New To Java...
# 19

> > A good way I think.. That is some thing thatnot only

> > the father knows his child, also child nows his father :)

>

> I don't think that's a good way. Superclasses should

> not depend on specific subclass implementations.

True; parent/child relationships don't model well in a Base/Derived

type of scheme. I am not my father, he just produced me. And if he

were still alive he would recognize me (parent.getChild()) but that

should be about it.

kind regards,

Jos (< "There! That's him! Officer! There he goes!" ;-)

JosAHa at 2007-7-9 21:08:28 > top of Java-index,Java Essentials,New To Java...