Results 1 to 5 of 5
- 08-19-2008, 06:48 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 4
- Rep Power
- 0
Calling a method on original class from created class
Hello,
I hope I worded the title correctly :)
I am making a small java.net wrapper where the main class creates an instance of the wrapper (JSock) and somewhere along the lines, when JSock gets a connection request it can call a method on the main class called connectionRequest()
So far i have this working fine but I have to always make sure the main class is called the same name (Logic) because the way I get the instance of the main class to call logic.connectionRequest(), I have to pass "this" into the constructor. i.e
JSock listenSock = new JSock(port, this)
then in the JSock I have "Logic parent" which contains the reference to the "parent" class.
Simplified code is...
class Logic
{
...
JSock listenSock = new JSock(port, this);
...
public void connectionRequest()
{
}
}
class JSock
{
Logic parent;
public JSock(Logic newParent)
{
parent = newParent;
}
...
parent.connectionRequest();
...
}
Is there a way to do this without needing to pass the instance of Logic when creating the class?
Super is only for derived classes isnt it?
Any help would be greatly appreciated!
regards,
Karsten
- 08-19-2008, 03:00 PM #2a way to do this without needing to pass the instance
Can there be more than one instance of the Logic class? If not, you could use the Singleton pattern -> have a static method of Logic that returns a reference to its one and only instance.
Another design, but it still requires passing a reference, is to use an interface to define the methods of Logic that you want to make available to JSock.
- 08-19-2008, 11:18 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 4
- Rep Power
- 0
hello,
Thankyou for your reply,
Passing a reference is cool, but ideally I wanted to distribute this wrapper to my uni mates so they can make network apps instantly, I just wanted to find a way so that they didn't have to call their main class 'Logic'.
I take it that in java there is no generic class that can accept anything like...
AnyClass parent;
rather than
Logic parent;
cheers,
- 08-20-2008, 12:19 AM #4
As I said before:
Another design, but it still requires passing a reference, is to use an interface to define the methods of Logic that you want to make available to JSock.
The Object class is parent to all other classes, but it's not much use in an application because you will want to have your own methods besides what Object provides.
- 08-20-2008, 01:25 AM #5
Member
- Join Date
- Jun 2008
- Posts
- 4
- Rep Power
- 0
Ideally the interface would be a good method, so I can simply do
class Logic implements JSock
{...
but because I intend to use JSock as a listen sock too... e.g.
JSock[] socks = new JSock[10];
JSock listenSock = new JSock(port);
...
listenSock.listen();
...
connectionRequest()
{
socks[count].accept(listenSock);
}
...
That would create a lot of instances of "this" (Logic) if, instead of JSock, Logic was used to create the socket wrapper class... so my uni chums will have to protect any code that they don't want running more than once, in the Logic class.
Meh, I guess they will have to then :)
Thankyou very much for your help, It is appreciated. I'll most probably use the interface solution that you suggested.
cheers,
Similar Threads
-
Calling a method in another class
By uncopywritable in forum New To JavaReplies: 9Last Post: 10-22-2012, 05:01 PM -
calling a public void method from a class button
By supa_kali_frajilistik in forum AWT / SwingReplies: 4Last Post: 05-23-2008, 02:05 PM -
calling a public void method from a class button
By supa_kali_frajilistik in forum AWT / SwingReplies: 1Last Post: 05-21-2008, 06:40 AM -
Calling method from another class
By asahli in forum New To JavaReplies: 1Last Post: 12-15-2007, 07:24 PM -
create a tree when a new class is created
By osval in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 09:58 PM
Bookmarks