Java extends classes - Share the extended class fields within the super class.

Posted by Bastan on Stack Overflow See other posts from Stack Overflow or by Bastan
Published on 2010-04-07T18:12:17Z Indexed on 2010/04/07 18:13 UTC
Read the original article Hit count: 185

Filed under:
|
|
|

Straight to the point... I have a class

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

Extended class:

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

MyClass:

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

I came to realize that MyClass can only be instantiated with (P_Gen thisObject) as opposed to (P extendedObject).

The situation is that I have code generated a bunch of classes like P_Gen. For each of them I have generated a class P which would contains my P specific custom methods and fields.

When I'll regenerate my code in the future, P would not be overwritten as P_Gen would.

** So what happened in my case???!!!... I realized that MyClass would beneficiate from the info stored in P in addition to only P_Gen. Would that possible?

  1. I know it's not JAVA "realistic" since another class that extends P_Gen might not have the same fields...
  2. BY DESIGN, P_Gen will not be extended by anything but P.... And that's where it kinda make sens. :-) at least in other programming language ;-)

In other programming language, it seems like P_Gen.this === P.this, in other word, "this" becomes a combination of P and P_Gen.

Is there a way to achieve this knowing that P_Gen won't be extended by anything than P?

© Stack Overflow or respective owner

Related posts about code-generation

Related posts about classes