Unimatrix_001
asked on
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()....
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()....
How is c2 created? You have to give the parent class as an argument to c2.
Eg. class2 should have an constructor like:
public class2( class1 parent );
of a method:
public void setParent( class1 parent );
public class2( class1 parent );
of a method:
public void setParent( class1 parent );
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();
}
}
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();
}
}
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();
}
}
class class1
{
public void somethingdone()
{
}
}
class class2
{
class c1;
public c2(class1 c1)
{
this c1=c1
}
public void somethingHappened()
{
c1.somethingdone();
}
}
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();
}
}
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();
}
}
> 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.
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.
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();
}
}
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();
}
}
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)
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)
That's 3 times the same ;°)
So we can really sure the solution is correct :-)
ASKER
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?
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?
>> 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.
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.
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()
E.g. an interface called Whatever having one function: public void somethingDone()
ASKER
"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?
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?
ASKER
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?...
How does then class2 then call this method?...
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();
}
}
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();
}
}
ASKER
testing...
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
http://www.javaspecialists.co.za/archive/Issue087.html
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
this.parent = parent;
}
So in the constructor of Class2 you can pass an instance of every Class that implements the Whatever interface
Reflection is indeed another possibilty, but personally I find using a common interface a neater and clearer way
ASKER
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)
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)
> 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.
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.
ASKER
"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.
unfortunately not available to me using 1.1! :( ...I should've mentioned...Sorry for that.
>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??
>>Class2(Class1)
Think you have some lower case upper case mix in your code??
>>... 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?
That smells like bad design, no?
Yeah, I always use(d) uppercases for classes and lower cases for instances
ASKER
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..
...nope, I just quickly typed that, just getting the error message across ;) So chancse are the caps are wrong there..
>> using 1.1!
Whow, that's ancient times! Is it a forced condition to use that one?
Whow, that's ancient times! Is it a forced condition to use that one?
ASKER
"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)
I do, the full error is:
Value for argument parent cannot be converted from Class1 into Class2.Class2(Whatever parent)
ASKER
"Whow, that's ancient times! Is it a forced condition to use that one?"
Unfortunately yes!!! ...
Unfortunately yes!!! ...
>> 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!
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!
Better post the whole thing again I'm afraid
Other remark if you write Class1 implements Whatever
it should also really implement it: it should have the somethingDone() function
it should also really implement it: it should have the somethingDone() function
> 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.
Then I think zzynx's suggestion is the one to go.
How are things going Unimatrix?
ASKER
Apologies....NTL is not the best ISP around *rolleyes*...
ASKER
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();
}
}
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();
}
}
Let me have a look...
ASKER
:)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
:°)))
Thank you
PS. You had
somethingDone(){
}
instead of
public void somethingDone() {
}
But I can't relate that to the error message...
Thank you
PS. You had
somethingDone(){
}
instead of
public void somethingDone() {
}
But I can't relate that to the error message...
ASKER
I think I'd better just stick to copying and pasting than trying to just type the code! ;)
:°D