Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Problem with designing threads & abstraction

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem with designing threads & abstraction

    Hi.

    I'm dealing a small program that has a SocketServer.
    I have a specific simple protocol that I'm implementing by myself.

    For the server I'm openeing a thread for every new session = socket
    (for a user connecting to the server).

    The problem is with the protocol design:
    I want to have an abstract class for "Message" - a protocol message between the server and
    the client.

    Tryed to create inside a "Session" class (which identifies each connection = independant socket)
    an abstract class for Message, so each session can instantiate new messages for every request (=message).

    The problem - something with the generics doesn't seem to work.
    These are the relevant headers:

    Session.java
    -------------------
    public class Session <M extends Message> extends Thread {
          .
          .
          public abstract class Message {
    		public abstract String treatMessage(InetAddress ip, int port);
    	}
          .
          .
    }

    Introduce.java
    -------------------
    public class Introduce extends Message {
     
    }

    inside "Introduce" class I get error messages from eclipse:
    "no enclosing instance of type Session<M> is available due to some intermediate constructor invocation."
    "Session.Message is a raw type. References to generic type Session<M>. Message should be parametrized."

    The design is in this way because all sessions share the same database, and this is the design I found
    for this situation (inner class). If any new good idea for design I'd love to hear..

    thanks..
    Last edited by Json; June 11th, 2010 at 07:26 AM. Reason: Please use code tags.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problem with designing threads & abstraction

    Java does not allow multiple inheritance. You can't extend more than one class.

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with designing threads & abstraction

    how am I extending more than one class ?
    you mean the inner class which extends "Message" while the whole class extends "Session" ?

    and do you have any other idea for that?

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problem with designing threads & abstraction

    public class Session <M extends Message> extends Thread {

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problem with designing threads & abstraction

    Quote Originally Posted by gilme View Post
    Hi.

    I'm dealing a small program that has a SocketServer.
    I have a specific simple protocol that I'm implementing by myself.

    For the server I'm openeing a thread for every new session = socket
    (for a user connecting to the server).

    The problem is with the protocol design:
    I want to have an abstract class for "Message" - a protocol message between the server and
    the client.

    Tryed to create inside a "Session" class (which identifies each connection = independant socket)
    an abstract class for Message, so each session can instantiate new messages for every request (=message).

    The problem - something with the generics doesn't seem to work.
    These are the relevant headers:

    Session.java
    -------------------
    public class Session <M extends Message> extends Thread {
          .
          .
          public abstract class Message {
    		public abstract String treatMessage(InetAddress ip, int port);
     
       // Did you ever create InetAddress in Message, or is in any super classes it might have?
       // did you define port in Message or superclass?
        // is there a way to use polymorphism to make a reference variable of superclass type point to subclass type if needed?
     
    // what are the <> for?
     
    // why is Message being defined inside of Session if it extends it instead of, if anything, Session being defined within Message?
     
     
     
     
     
    	}
          .
     
          .
    }

    Introduce.java
    -------------------
    public class Introduce extends Message {
     
    // were any parameters overridden from Message and not referenced with super?
    // did you have ever define a method called treatMessage in Introduce, with same parameters and everything?
     
     
     
     
    }

    inside "Introduce" class I get error messages from eclipse:
    "no enclosing instance of type Session<M> is available due to some intermediate constructor invocation."
    "Session.Message is a raw type. References to generic type Session<M>. Message should be parametrized."

    The design is in this way because all sessions share the same database, and this is the design I found
    for this situation (inner class). If any new good idea for design I'd love to hear..

    thanks..
    What class is extending what?

    Also, in theory, could you do this,

    public class superClass
    {

    public class subClass extends superClass
    {

    public class subClassTwo extends subClass
    {

    }
    }
    }


    }

    Also, you need to define class InetMessage, unless it's predefined, don't know on that one.

    If it's not predefined, then perhaps you could do this:

    public class superClass extends InetMessage
    {

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problem with designing threads & abstraction

    Wait, I''m not sure you can define a class as an inner class inside of the class it''s extending.

    However, I think you can have two inner classes like this:

    public class innnerClassOne{

    }

    public class innerClassTwo extends innerClassOne
    {

    }

  7. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Problem with designing threads & abstraction

    Your class declaration is fine.

    public class Session<M extends Message> extends Thread

    However you need to create a new file called Message.java and put your Message class in there instead which should solve your problem.

    public abstract class Message {
        public abstract String treatMessage(InetAddress ip, int port);
    }

    // Json

Similar Threads

  1. [SOLVED] Questions About Threads
    By neo_2010 in forum Threads
    Replies: 4
    Last Post: March 15th, 2010, 09:04 AM
  2. Problem in JSP Page Designing
    By vinothkumarrvk in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: March 10th, 2010, 01:09 AM
  3. threads in gui
    By urosz in forum AWT / Java Swing
    Replies: 1
    Last Post: November 3rd, 2009, 05:20 PM
  4. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM