Results 1 to 4 of 4
Thread: Interface Input
- 10-14-2011, 10:57 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
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.
-
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-14-2011 at 11:15 PM.
- 10-15-2011, 01:00 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 18
- Rep Power
- 0
Re: Interface Input
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.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 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.
-
Re: Interface Input
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
-
How do I creat a function that receive input and limits the input time
By itayjc in forum New To JavaReplies: 0Last Post: 10-14-2011, 12:33 AM -
Input technique for unknown lines of input
By ducreative in forum New To JavaReplies: 16Last Post: 09-23-2009, 09:26 AM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks