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]

Make the Child class extend the Father class.Message was edited by: Djaunl
pass the father to the child when you make a child.
Look at this too: http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html
> Make the Child class extend the Father class.Sure, do that and wait for an OutOfMemoryError or a StackOverFlowErrorwhichever comes first ;-)kind regards,Jos
> > 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?
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...
> Actually, why would that happen?Because the father tries to create a child, it will become recursive.Kaj
> 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
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
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..
> > 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?
> > > 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");
}
}
> 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?
> 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
> > 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.
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
> 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.~
> I don't think that's a good way. Superclasses should> not depend on specific subclass implementations.++
> > 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!" ;-)