views:

70

answers:

3

EDITED:

Straight to the point... I have a class

public class P_Gen{  
    protected String s;
    protected Object oP_Gen;
    protected Object oP_Gen2;
    public P_Gen(String str){
       s = str;
       oP_Gen = new MyClass(this);
       oP_Gen2 = new MyClass2(this);
    }
}  

Extended class:

public class P extends P_Gen{  
    protected Object oP;  
    public P(String str){    
       super(str);  
       oP = new MyClass(this);
    }
} 

MyClass:

public class MyClass{  
    protected Object oMC;
    public MyClass(P extendedObject){
       this.oMC = extendedObject.oP;
    }
}

MyClass2:

public class MyClass2{  
    protected Object oMC2;
    public MyClass(P_Gen thisObject){
       this.oMC2 = thisObject.oP;
    }
}   

The Class P_Gen shown above gives me an error for line :

oP_Gen = new MyClass(this);  

stating "cannot find symbol constructor MyClass(P_Gen)."

What i want to achieve is to make P.oP available from MyClass2.

My initial thought were that P.this === P_Gen.this. In other word, P_Gen disappear when called from super() and what is left is only P the extended class.

A: 

I'm a bit confused by your question (and your code, which doesn't have a doSomething() method anywhere), but you might benefit from P_Gen being an abstract class with an abstract method doSomething(). Then P can implement that method, and anything with a reference to a P_Gen can call it (and get the implementation in P, if that's what instance it is).

nojo
I have edited the entire question after doing some tests on my side. Hope It make more sens.
Bastan
Followup question: are P_Gen.oP_Gen and P.oP intended to be the same object? Or does an instance of P need to have an oP and an oP_Gen?
nojo
What I'm getting at with that last question: if they're intended to be the same object, then P shouldn't declare the oP member, and you can access oP_Gen without any casting.
nojo
No, not the same. In fact, if i could, P_Gen.oP_Gen would not exist nor would P_Gen.PGen2, P_Gen would simply use P.oP like this.oP from within P_Gen. Confused enough????!!!!
Bastan
Ok ok, I have a bunch of MyClass (1....x). They all need to be instantiated from within P_Gen. But i want them to have access to P, not only P_Gen. Is that possible?
Bastan
I'M thinking of doing it in two step. From the P constructor i would call super(...); then call super.init(this); then all MyClass(1...x) would be instantiated with P instead of P_Gen; MyClass(1...x) could keep a reference to P if needed... Sounds weird but feasible, i'll give it a try.
Bastan
The best solution is to not instantiate MyClass from within P_Gen, but if you're hands are tied, how about using `instanceof` and casting, for example: if (this instanceof P) { oP_Gen = new MyClass((P)this); } else { /* do something else */ }
Kevin K
A: 

ok,

  1. In the class MyClass2, the constructor should be named 'MyClass2'
  2. Why dont you tray to define the constructors like this:
public MyClass(P_Gen thisObject){...}
public MyClass2(P_Gen thisObject){...}

Doing this, you are able to do something like:

new MyClass(new P());

Also you need to cast thisObject to P inside MyClass and MyClass2 contructors, so then you are able to get the oP variable. My recommendation 4you is to reed a little bit more about java inheritance.

JP

juanp
A: 

After a good night sleep.... It is not what I wanted to do that is wrong but my approach altogether. I went back to my beginning of OOP. Shame on me. Anyway, i gave the answer to @juanp because he said "My recommendation 4you is to reed a little bit more about java inheritance.".

For those who are interested, here is what it should look like:

The generated P_Gen class:

public abstract class P_Gen{
    protected String s;
    protected Object oP;
    public P_Gen(String str){
        this.s = str;
        init(str);
    }

    protected void init(String str){
        if(this instanceof P){
            oP = new MyClass((P)this);
        }
    }
}

The extended P class:

public class P extends P_Gen{  
    public String sSTR;
    public P(String str){
       super(str);
    }

    @Override
    protected void init(String str){
        this.sSTR = str;
        this.oP = new MyClass(this);
    }
}

The MyClass class:

public class MyClass{
    protected Object oMC;
    public MyClass(P extendedObject){
       this.oMC = extendedObject.sSTR;
    }
}

What I did was to move the MyClass instantiation from the constructor to an init function. That init function is then overridden in the extended class..... very basic stuff i know.

Even more basic, added abstract to my P_Gen class to prevent it from being instantiated directly. I could also declare an abstract init method in P_Gen.

All this makes me think that maybe .... :-) ...another day. Thanks for helping me get back on track.

Bastan