Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-19-2008, 07:48 AM
Member
 
Join Date: Jun 2008
Posts: 4
kpedersen is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-19-2008, 04:00 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
Quote:
a way to do this without needing to pass the instance
What is wrong with passing a reference of 'this'? How else with the JSock class get a reference to Logic?
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-20-2008, 12:18 AM
Member
 
Join Date: Jun 2008
Posts: 4
kpedersen is on a distinguished road
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,
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-20-2008, 01:19 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
As I said before:
Quote:
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.
How do you want to use the parent variable? What use is it if it is not some "type" like a Logic object? Your code shows it expects the parent object to have a method: parent.connectionRequest(); This method must be defined in an interface or in another class. It's not magic!

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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-20-2008, 02:25 AM
Member
 
Join Date: Jun 2008
Posts: 4
kpedersen is on a distinguished road
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,
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a public void method from a class button supa_kali_frajilistik AWT / Swing 4 05-23-2008 03:05 PM
calling a public void method from a class button supa_kali_frajilistik AWT / Swing 1 05-21-2008 07:40 AM
Calling a method in another class uncopywritable New To Java 8 01-14-2008 06:37 PM
Calling method from another class asahli New To Java 1 12-15-2007 08:24 PM
create a tree when a new class is created osval Advanced Java 1 08-06-2007 10:58 PM


All times are GMT +3. The time now is 09:05 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org