Results 1 to 4 of 4

Thread: Interface Input

  1. #1
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default Interface Input

    How would you input values from the keyboard into an interface that can be used by a hierarchy of classes?

    I want to input a string "name" from the keyboard into an interface so that classes that implement the interface can use the name to output different messages to the user.

    I'm confused because an interface contains constants, not variables.
    I know I can't use a buffered reader or scanner in an interface.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Interface Input

    This question doesn't make sense to me as interfaces aren't used like that. Think of an interface as a blueprint, not as a house. You can't walk in the door of a blueprint since it's not concrete. Similarly you can't really input values into an interface. If you're trying to do so, then your design may be completely messed up.

    Can you clarify your actual overall problem and not how you intend to solve it by code as I have a feeling we need to approach the solution completely differently.
    Last edited by Fubarable; 10-15-2011 at 12:15 AM.

  3. #3
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default Re: Interface Input

    Java Code:
    import java.io.*;
    public class Messages
    {
      public static void main (String[] args) 
      {
        BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
        System.out.println("Enter recipient's name:"); 
        String recipient = reader.readLine();
        
        System.out.println("Enter sender's name:"); 
        String sender = reader.readLine();
        
        //Names message1 = new Greeting(recipient, sender); ?
        //System.out.println(message1.printmsg());
        
      } 
    }
    
    interface Names
    {
      final String recipient; //
      final String sender; //
      void printmsg();
      
    }
    
    abstract class MessageTypes implements Names
    {
      String recipient;
      String sender;
      protected abstract void printmsg();
    }
    
    class Greeting extends MessageTypes
    {
      Greeting (String r, String s)
      {
        recipient = r;
        sender = s;
      }
      
      public void printmsg()
      {
        System.out.println("Hello " + recipient + ", says " + sender);
      }
    }
    I will output to the screen using an array of objects once my instructor tells us all the classes required. This is only an exercise, but it will be built off of next week.
    I was told that we should be able to input into the program, and the only way I could think of doing that would be to put those inputs into the interface.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Interface Input

    Quote Originally Posted by wizar View Post
    Java Code:
    import java.io.*;
    public class Messages
    {
      public static void main (String[] args) 
      {
        BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
        System.out.println("Enter recipient's name:"); 
        String recipient = reader.readLine();
        
        System.out.println("Enter sender's name:"); 
        String sender = reader.readLine();
        
        //Names message1 = new Greeting(recipient, sender); ?
        //System.out.println(message1.printmsg());
        
      } 
    }
    
    interface Names
    {
      final String recipient; //
      final String sender; //
      void printmsg();
      
    }
    
    abstract class MessageTypes implements Names
    {
      String recipient;
      String sender;
      protected abstract void printmsg();
    }
    
    class Greeting extends MessageTypes
    {
      Greeting (String r, String s)
      {
        recipient = r;
        sender = s;
      }
      
      public void printmsg()
      {
        System.out.println("Hello " + recipient + ", says " + sender);
      }
    }
    I will output to the screen using an array of objects once my instructor tells us all the classes required. This is only an exercise, but it will be built off of next week.
    I was told that we should be able to input into the program, and the only way I could think of doing that would be to put those inputs into the interface.
    No, no input should be written "into" the interface, as the interface is a contract and once set cannot be changed. It only defines what its child classes must do, nothing more and nothing less. Your user input and output will be in a completely different class, one that doesn't inherit either from your interface or its child abstract class, but one that contains an instance of a class that inherits from these.

    A forum lesson to take from this is that you should tell us what you're trying to do, not so much how you're trying to do it because you're barking up the absolute wrong tree.

Similar Threads

  1. Replies: 0
    Last Post: 10-14-2011, 01:33 AM
  2. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 10:26 AM
  3. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 09:46 AM

Posting Permissions

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