Complicated notify parent thing?

Hi all,

I've got two classes:

public class class1{
    private class2 c2;

    somethingDone(){
        //Called by c2.
    }
}

public class class2{
    public void somethingHappened(){
       //Call parent method:
       somethingDone();
    }
}



class2 manages itself when something happens in class2, it calls the somethingHappened() class2 method, which in turn I want to call the parent somethingDone method...The problem I have is, is that class2 doesn't know anything about class1, since the actual class that uses class2 can vary. All that class2 should know is that it must call the parent method somethingDone()....
LVL 3
Unimatrix_001Asked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
Make three files

1) Whatever.java containing:

//----------------------------------------------
public interface Whatever {
    public void somethingDone();
}
//----------------------------------------------

2) Class1.java containing:
//----------------------------------------------
public class Class1 implements Whatever{
    private Class2 c2;

    public Class1(){
        c2=new Class2(this);
    }

    public void somethingDone() {
    }
}
//----------------------------------------------

3) Class2.java containing:
//----------------------------------------------
public class Class2 {

   private Whatever parent;

   public Class2(Whatever parent){
        this.parent=parent;
   }

   public void somethingHappened(){
        parent.somethingDone();
   }
}
//----------------------------------------------

These compile all OK.

When do you get that error? At compile time?
What is the code you test with?
0
 
JugglerWCommented:
How is c2 created? You have to give the parent class as an argument to c2.
0
 
JugglerWCommented:
Eg. class2 should have an constructor like:

public class2( class1 parent );

of a method:

public void setParent( class1 parent );
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
sudhakar_koundinyaCommented:
If i am understand u correctly this is your solution

public class class1{
    private class2 c2;

    protected somethingDone(){
        //Called by c2.
    }
}

public class class2{
    public void somethingHappened(){
       //Call parent method:
       somethingDone();
    }
}


or

public class class2{
    public void somethingHappened(){
       //Call parent method:
      super.somethingDone();
    }
}

0
 
sudhakar_koundinyaCommented:
opps  seems didn't understand correctly

class class1
{
    public void somethingdone()
   {
   }
}


class class2
{
   class c1;
    public  c2(class1 c1)
    {
         this c1=c1
     }
    public void somethingHappened()
   {
     c1.somethingdone();
   }
}
0
 
JugglerWCommented:
So may look like:

public class class1{
    private class2 c2;

    public class1( )
    {
         c2 = new class2( this );
    }

    somethingDone(){
        //Called by c2.
    }
}

public class class2{
    class1 parent;

    public class2( class1 parent )
    {
        this.parent = parent;
    }
    public void somethingHappened(){
       //Call parent method:
       if ( parent != null )
           parent.somethingDone();
    }
}
0
 
girionisCommented:
> All that class2 should know is that it must call the parent method somethingDone()....

somethingDone() is not parent's method since class2 does not extend class1. If you make it parents class then your class2 needs to extend class1. Then follow sudhakar_koundinya comment.
0
 
zzynxSoftware engineerCommented:
I think you might mean this:

public class Class1 {
    private Class2 c2;

    public Class1() {
        c2 = new Class2(this);
    }

    public void somethingDone() {
         // blablabla
    }
}


public class Class2 {

     private Class1 c1;

     public Class2(Class1 c1) {
        this.c1 = c1;
     }

     public void somethingHappened() {
          // blablablabla
          c1.somethingDone();
     }
}
0
 
JugglerWCommented:
Perhaps you can make the method somethingDone() static in class1?
Like:

public class class1{
    private class2 c2;

    public class1( )
    {
         c2 = new class2( this );
    }

    static public void somethingDone(){
        //Called by c2.
    }
}

So you can call from class2 by
  class1.somethingDone();

BTW: Please note there is a convention in Java to start class names with an upper case letter (e.g. Class1 and Class2 not class1 or class2)
0
 
zzynxSoftware engineerCommented:
That's 3 times the same ;°)
0
 
JugglerWCommented:
So we can really sure the solution is correct :-)
0
 
Unimatrix_001Author Commented:
Hi guys....WOW! That's certainly a few responses, although not all of them so helpful! ;) I'm well aware of doing the following:


public class class1{
    private class2 c2;

    somethingDone(){
        //Called by c2.
    }
}

public class class2{
    private class1 c1;
    public void somethingHappened(){
       //Call parent method:
       c1.somethingDone();
    }
}


BUT!!!....If you read the question...

"The problem I have is, is that class2 doesn't know anything about class1, since the actual class that uses class2 can vary. All that class2 should know is that it must call the parent method somethingDone()...."

So the above code fragment which has been suggested quite a bit, could NOT be used since that is then limiting class2 to just calling the parent in class1. I.e. what if I used the above code for one program but my friend had the following:


public class class3{
    private class2 c2;

    somethingDone(){
        //Called by c2.
    }
}

public class class2{
    public void somethingHappened(){
       //Call parent method:
       somethingDone();
    }
}


He doesn't want to go having to change class2 just to reflect his new class3....See my point?
0
 
zzynxSoftware engineerCommented:
>> class2 doesn't know anything about class1
Well, at least it knows that Class2 has a method somethingDone() that must be called.
And since that method has to be called on something it should also have an instance to call it on (that's the Class1 instance/parent we pass in it's constructor.)

Or indeed, if it is a static function it doesn't need an instance.
0
 
zzynxSoftware engineerCommented:
Well if both Class1 and Class3 implement the same interface it can be done

E.g. an interface called Whatever having one function: public void somethingDone()
0
 
Unimatrix_001Author Commented:
"Well, at least it knows that Class2 has a method somethingDone() that must be called."
True.

And it's the part where the instance comes in, is why this question is here ;) I can't pass in a class1 instance because somewhere down the line I may decide to use this with a class3 or class4, and I don't want to be going back to months old code just to change the constructor...See?
0
 
Unimatrix_001Author Commented:
Ah right!...Ok, so class1 and any other classes that use class3 that use class2 must have a common interface?

How does then class2 then call this method?...
0
 
zzynxSoftware engineerCommented:
public class Class1 implements Whatever {

   
    public void dsomethingDone() {
    }
}

public class Class3 implements Whatever {

    public void somethingDone() {
    }
}

public class Class2 {

    private Whatever parent;

   public Class2(Whatever parent) {
      this.parent = parent;
   }

   public void somethingHappened() {

       //Call parent method:
       parent.somethingDone();
    }

}
0
 
Unimatrix_001Author Commented:
testing...
0
 
girionisCommented:
Your best bet I reckon is to use the sun.reflect.Reflection package and specifically the getCallerClass(int i) method to find out who is called your class. Then just use this class to call its somethingDone() method:

http://www.javaspecialists.co.za/archive/Issue087.html
0
 
zzynxSoftware engineerCommented:
public Class2(Whatever parent) {
      this.parent = parent;
   }


So in the constructor of Class2 you can pass an instance of every Class that implements the Whatever interface
0
 
zzynxSoftware engineerCommented:
Reflection is indeed another possibilty, but personally I find using a common interface a neater and clearer way
0
 
Unimatrix_001Author Commented:
zzynx: It's not having it...I'm doing:

public class class1 implements Whatever{
    Class2 c2=new Class2(this);
}

public class class2{
    Whatever parent;
    public class2(Whatever parent){
        this.parent=parent;
    }
}


It's complaining that the constructor of class2 is not of the type:

Class2(Class1)
0
 
girionisCommented:
> Reflection is indeed another possibilty, but personally I find using a common interface a neater and clearer way

There could be problems if several classes implement the same interface though as the jvm would have to decide during runtime which one is the "best" implementation of the somethingDone method. And it might not be the class we want.
0
 
Unimatrix_001Author Commented:
"Reflection is indeed another possibilty, but personally I find using a common interface a neater and clearer way"
unfortunately not available to me using 1.1! :( ...I should've mentioned...Sorry for that.
0
 
JugglerWCommented:
>It's complaining that the constructor of >>class2 is not of the type:

>>Class2(Class1)

Think you have some lower case upper case mix in your code??
0
 
zzynxSoftware engineerCommented:
>>... as the jvm would have to decide during runtime which one is the "best" implementation of the somethingDone method. And it might not be the class we want.
That smells like bad design, no?
0
 
zzynxSoftware engineerCommented:
Yeah, I always use(d) uppercases for classes and lower cases for instances
0
 
Unimatrix_001Author Commented:
Think you have some lower case upper case mix in your code??
...nope, I just quickly typed that, just getting the error message across ;) So chancse are the caps are wrong there..
0
 
zzynxSoftware engineerCommented:
>> using 1.1!
Whow, that's ancient times! Is it a forced condition to use that one?
0
 
Unimatrix_001Author Commented:
"Yeah, I always use(d) uppercases for classes and lower cases for instances"
I do, the full error is:

Value for argument parent cannot be converted from Class1 into Class2.Class2(Whatever parent)
0
 
Unimatrix_001Author Commented:
"Whow, that's ancient times! Is it a forced condition to use that one?"
Unfortunately yes!!! ...
0
 
zzynxSoftware engineerCommented:
>> I do
No you don't ;°)

public class class1 implements Whatever{       // <<<<<<<<< better use upper case Class1
    Class2 c2=new Class2(this);
}

public class class2{                            // <<<<<<<<< lower case should be upper case
    Whatever parent;
    public class2(Whatever parent){     // <<<<<<<<< idem
        this.parent=parent;
    }
}

>>Unfortunately yes!!! .
Weird!
0
 
zzynxSoftware engineerCommented:
Better post the whole thing again I'm afraid
0
 
zzynxSoftware engineerCommented:
Other remark if you write Class1 implements Whatever
it should also really implement it: it should have the somethingDone() function
0
 
girionisCommented:
> unfortunately not available to me using 1.1! :( ...I should've mentioned...Sorry for that.

Then I think zzynx's suggestion is the one to go.
0
 
zzynxSoftware engineerCommented:
How are things going Unimatrix?
0
 
Unimatrix_001Author Commented:
Apologies....NTL is not the best ISP around *rolleyes*...
0
 
Unimatrix_001Author Commented:
No you don't ;°)
Well alrighty, I do in my actual code! ;)

"Other remark if you write Class1 implements Whatever
it should also really implement it: it should have the somethingDone() function"
Already done! :)

">>Unfortunately yes!!! .
Weird!"
Browser issues ;)


The error I'm having is:
Value for argument parent cannot be converted from Class1 into Class2.Class2(Whatever parent)

With the code:

public class Class1 implements Whatever{
    private Class2 c2;

    public Class1(){
        c2=new Class2(this);
    }

    somethingDone(){
    }
}


public class Class2{

   Whatever parent;

    public Class2(Whatever parent){
        this.parent=parent;
    }

    public void somethingHappened(){
        parent.somethingDone();
    }
}
0
 
zzynxSoftware engineerCommented:
Let me have a look...
0
 
Unimatrix_001Author Commented:
:)
0
 
Unimatrix_001Author Commented:
Hmm...that's working fine! :S ...I can't see for the life of me what's different that you've got but oh well!...points to zzynx.
0
 
zzynxSoftware engineerCommented:
:°)))
Thank you


PS. You had

    somethingDone(){
    }

instead of

   public void somethingDone() {
   }

But I can't relate that to the error message...
0
 
Unimatrix_001Author Commented:
I think I'd better just stick to copying and pasting than trying to just type the code! ;)
0
 
zzynxSoftware engineerCommented:
:°D
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.