Results 1 to 15 of 15
Thread: Inheritance
- 11-18-2009, 03:07 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
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:
- 11-18-2009, 06:17 PM #2
Member
- Join Date
- Oct 2009
- Location
- Rotterdam
- Posts
- 52
- Rep Power
- 0
I really don't get what you mean.
If you declare something like this:You can initialize it like this:Java Code:A a;
Java Code:a = new A1();
- 11-18-2009, 06:33 PM #3
B is a factory class. So: Inherit B, overwrite getReftoA(), and have it return A1.
My Hobby Project: LegacyClone
- 11-18-2009, 09:59 PM #4
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 confusedJava 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 }
class B instantiates A so this should helpLast edited by aaroncarpet; 11-18-2009 at 10:47 PM.
- 11-18-2009, 10:28 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
- 11-18-2009, 10:34 PM #6
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 descriptionLast edited by aaroncarpet; 11-18-2009 at 10:43 PM.
- 11-18-2009, 11:55 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
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.
- 11-19-2009, 09:42 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 11-19-2009, 01:02 PM #9
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
- 11-19-2009, 01:28 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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-19-2009, 01:30 PM #11
but we thought you could only use class B to access to access A
- 11-19-2009, 01:52 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 11-19-2009, 08:39 PM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
> 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.
- 11-20-2009, 07:53 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 11-20-2009, 10:41 AM #15
Member
- Join Date
- Nov 2009
- Location
- Abuja, Nigeria
- Posts
- 15
- Rep Power
- 0
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
-
Inheritance problems
By PVL268 in forum New To JavaReplies: 3Last Post: 03-23-2009, 02:52 AM -
Inheritance example
By kris4u4ever in forum New To JavaReplies: 3Last Post: 03-21-2009, 02:53 PM -
inheritance
By itaipee in forum New To JavaReplies: 6Last Post: 01-20-2009, 08:18 PM -
Inheritance
By mew in forum New To JavaReplies: 1Last Post: 12-07-2007, 06:08 PM -
Inheritance in GUI
By Marty in forum SWT / JFaceReplies: 2Last Post: 05-11-2007, 12:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks