Results 1 to 5 of 5
  1. #1
    kpedersen is offline Member
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default

    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.

  3. #3
    kpedersen is offline Member
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Default

    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,

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default

    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.
    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.

  5. #5
    kpedersen is offline Member
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Default

    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

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 04:01 PM
  2. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 4
    Last Post: 05-23-2008, 01:05 PM
  3. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 1
    Last Post: 05-21-2008, 05:40 AM
  4. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 06:24 PM
  5. create a tree when a new class is created
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:58 PM

Posting Permissions

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