?
Solved

Complicated notify parent thing?

Posted on 2004-08-07
44
Medium Priority
?
227 Views
Last Modified: 2010-03-31
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()....
0
Comment
Question by:Unimatrix_001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 18
  • 14
  • 6
  • +2
44 Comments
 
LVL 7

Expert Comment

by:JugglerW
ID: 11744078
How is c2 created? You have to give the parent class as an argument to c2.
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11744081
Eg. class2 should have an constructor like:

public class2( class1 parent );

of a method:

public void setParent( class1 parent );
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11744082
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11744090
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
 
LVL 7

Expert Comment

by:JugglerW
ID: 11744091
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
 
LVL 35

Expert Comment

by:girionis
ID: 11744099
> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744101
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
 
LVL 7

Expert Comment

by:JugglerW
ID: 11744102
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744107
That's 3 times the same ;°)
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11744116
So we can really sure the solution is correct :-)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744123
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744125
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744131
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
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744133
"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
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744136
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744139
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
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744142
testing...
0
 
LVL 35

Expert Comment

by:girionis
ID: 11744155
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744159
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744168
Reflection is indeed another possibilty, but personally I find using a common interface a neater and clearer way
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744174
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
 
LVL 35

Expert Comment

by:girionis
ID: 11744175
> 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
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744177
"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
 
LVL 7

Expert Comment

by:JugglerW
ID: 11744184
>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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744185
>>... 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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744190
Yeah, I always use(d) uppercases for classes and lower cases for instances
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744191
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744197
>> using 1.1!
Whow, that's ancient times! Is it a forced condition to use that one?
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744199
"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
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744203
"Whow, that's ancient times! Is it a forced condition to use that one?"
Unfortunately yes!!! ...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11744212
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744217
Better post the whole thing again I'm afraid
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11744223
Other remark if you write Class1 implements Whatever
it should also really implement it: it should have the somethingDone() function
0
 
LVL 35

Expert Comment

by:girionis
ID: 11744232
> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744299
How are things going Unimatrix?
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744402
Apologies....NTL is not the best ISP around *rolleyes*...
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744426
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744460
Let me have a look...
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744465
:)
0
 
LVL 37

Accepted Solution

by:
zzynx earned 2000 total points
ID: 11744483
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
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744495
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
 
LVL 37

Expert Comment

by:zzynx
ID: 11744506
:°)))
Thank you


PS. You had

    somethingDone(){
    }

instead of

   public void somethingDone() {
   }

But I can't relate that to the error message...
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11744509
I think I'd better just stick to copying and pasting than trying to just type the code! ;)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11744579
:°D
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question