Results 1 to 15 of 15

Thread: Inheritance

  1. #1
    gpio is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Question Inheritance

    Hello,

    There is a class A which i want to extend in A1.
    In order to get a reference to A you must call a method from class B

    A a = b.getReftoA()

    Also b (an instance of B) is given directly from a tool API so I can not override B in order getReftoA to return an A1 object.

    Any ideas on how I can create instances of A1 ????

    Thx
    go:mad:

  2. #2
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    I really don't get what you mean.

    If you declare something like this:
    Java Code:
    A a;
    You can initialize it like this:
    Java Code:
    a = new A1();

  3. #3
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    B is a factory class. So: Inherit B, overwrite getReftoA(), and have it return A1.
    My Hobby Project: LegacyClone

  4. #4
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    Java Code:
    import  A;
    import B;
    
    public class C extends B{  //as the above poster you will have to overide b methods 
    A a; A1 a1;
    public void C(){a = this.getReftoA(); 
     a1 = new A1();}
    
    public static void main (string args[]) 
       { C myC = new C();
    
    
          // myC.dowhatevertoa1();
       }
    }
    
    
    public class A1 extends A{ //better to make this class in a separate file and use the package 
    
    // overide and add methods here
    // use the super
    }
    If you can call methods from B then it isn't private If you exend the private class then you have private access because you are that private class...can a private class be extended? now I am confused

    class B instantiates A so this should help
    Last edited by aaroncarpet; 11-18-2009 at 10:47 PM.

  5. #5
    gpio is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by mrmatt1111 View Post
    B is a factory class. So: Inherit B, overwrite getReftoA(), and have it return A1.
    Thx thats also is my idea BUT

    B constructor is called by the framework and not from my code.
    Then an instance of B is passed as an argument to my main method.
    And as far I know there is now chance to say to the framework in order to pass me B, to pass me B1 ....

    Thx

  6. #6
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    then overide the constructor...if b is a java class give the name and I will look it up. sounds like maybe you need an interface
    you can use super after you extend b please look at my code;

    b.getreftoA(); .......this does not look right

    maybe b.getrefto(A);??????????


    post up the code for class B

    framework is to vague of a description
    Last edited by aaroncarpet; 11-18-2009 at 10:43 PM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Is class A final or does it have no accessible constructor? If so you can't extend it. If it isn't then extend it and construct your A1 instance by using a constructor that you write for that class. (Ie in that case the factory class has nothing to do with it.)

    Your actual problem (rather than your proposed solution) is not clear.

    [Edit] A guess, but perhaps A1 should not extend A so much as wrap one. That is construct A1 having an instance of A as a member field. Initialise that field using the framework's factory method. And give the A1 class whatever behaviour the A instance provides and whatever other behaviour you want.
    Last edited by pbrockway2; 11-19-2009 at 12:01 AM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    So, you have no control over B at all?
    Is that what you're saying?
    And you want to change what B does?

    Sorry...impossible requirements.

    ETA: pbrockway2's wrapper is the only possible solution to this, but I suspect that's not the answer you're looking for.

  9. #9
    gpio is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    So, you have no control over B at all?
    Is that what you're saying?
    And you want to change what B does?

    Sorry...impossible requirements.

    ETA: pbrockway2's wrapper is the only possible solution to this, but I suspect that's not the answer you're looking for.
    What if I wrap & inherit :

    A1 extends A{

    public A a;

    public overideMethodfromA(){}

    }

    and I will use A1 only for the method I want to overide and for all the other methods I will use the superclass inheretited methods. ???

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Do you need to have A1 be an A?
    If so, then you this would work, except you'd have to override all of A's methods, and have them call 'a'. It strikes me as a nasty hack, though.

  11. #11
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    but we thought you could only use class B to access to access A

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    I think the intention is to get the A from B, then wrap it in an A1...which itself extends A. Like a disguise.

    As I said, it looks rather hacky to me.

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    > As I said, it looks rather hacky to me.

    If A is extendable then the framework's authors expect some such thing to be done. (Assuming we have an accurate account of the starting point...)

    It's a bit irksome that you have to write out all the A1 methods but that's a "feature" of Java. In go, for instance, this sort of thing seems commonplace and expected: the wrapped methods can be invoked as if they were methods of the wrapper.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Considering A is supposedly only accessible via B I would argue that it was not the intention to extend A, but an oversight. Otherwise the framework should provide a way of asking for a particular A, or it should itself be capable of making that decision.

    As I said, it's rather hacky.

  15. #15
    lolu is offline Member
    Join Date
    Nov 2009
    Location
    Abuja, Nigeria
    Posts
    15
    Rep Power
    0

    Default

    Hmmm, really tacky.

    The way I see it, A may be a kind of interface. if you extend A1 from A, then you would need to define a method in A that would help you instantiate A1 of which is impossible since you do not have control over A. Coz if you could get to the source code, add your own method to A that can help you get A1.(Did I make sense here? ) Otherwise:

    How complicated is the task you want to carry out? You can alternatively write your own A and extend it to A1. Then import it into your code and have whatever values it returns passed to the A that you got from B.


    If it cant work, then there has to be way A and B are working together with other classes and interfaces that you might have to find out and involve.

Similar Threads

  1. Inheritance problems
    By PVL268 in forum New To Java
    Replies: 3
    Last Post: 03-23-2009, 02:52 AM
  2. Inheritance example
    By kris4u4ever in forum New To Java
    Replies: 3
    Last Post: 03-21-2009, 02:53 PM
  3. inheritance
    By itaipee in forum New To Java
    Replies: 6
    Last Post: 01-20-2009, 08:18 PM
  4. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 06:08 PM
  5. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 12:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •