Results 1 to 8 of 8
- 11-04-2012, 08:06 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
How to store user input from subclass into variables from superclass
This is an example of what the superclass that I've been given looks like (I'm not allowed to change anything about it):
The goal of my program is to ask the user a question in the subclass, such as "What is your dog's name?", and get their input. I'm supposed to inherit the methods/variables from the superclass, store the user's input in the superclass' variable, and then output it.Java Code:public class Superclass { private String answer; public Superclass(String reply) { answer = reply; } public Superclass() { answer = ""; } public void setAnswer(String reply) { answer = reply; } public String getAnswer() { return answer; }
I'm very confused about what all those methods in the superclass do, especially the second Superclass method. I'm also not sure which ones I can bring into my subclass.Java Code:System.out.print("Pleae enter the variable: "); // I've been using "input.nextLine()" to get the user input, but can't get the input assigned to the superclass variable
I see that the superclass uses getter/setter methods, but I don't understand how to utilize them in my class.
Thank you.
- MCLast edited by mc54321; 11-04-2012 at 08:08 PM.
- 11-04-2012, 08:12 PM #2
Member
- Join Date
- Sep 2012
- Posts
- 34
- Rep Power
- 0
Re: How to store user input from subclass into variables from superclass
you define a subclass, which uses the extends keyword.
the methods which declared public are accesible for the subclass, the private variable isn't.Java Code:class SubClass extends Superclass
you need to use the getter/setter methods to access the private variable.
which will use the setAnswer method to change the private variable..Java Code:SubClass ins = new SubClass(); ins.setAnswer(answer);
- 11-04-2012, 09:28 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Re: How to store user input from subclass into variables from superclass
Using that, I get an error message.
I had already had an instantiation written in my program, looking like:Java Code:ins.setAnswer(answer); // The error message says that "answer" is not visible
Should that be kept in? Or does Subclass ins = new Subclass(); do the same thing?Java Code:Subclass x = new Subclass();
-
Re: How to store user input from subclass into variables from superclass
don't use the key word answer. Instead pass any String into the setAnswer(...) method:
Java Code:Subclass sub = new Subclass(); sub.setAnswer("Foo"); system.out.println(sub.getAnswer());
- 11-04-2012, 09:46 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 34
- Rep Power
- 0
Re: How to store user input from subclass into variables from superclass
yeah as Fubarable said, i didn't mean that answer is the variable in the superclass, just a local variable..
- 11-04-2012, 09:50 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Re: How to store user input from subclass into variables from superclass
Thank you. That helped a lot. Now the only thing is, how can I get it to output the user's input instead of "Foo"?
-
Re: How to store user input from subclass into variables from superclass
- 11-04-2012, 11:04 PM #8
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
I need help learning how to get user input and store a array
By justintime in forum New To JavaReplies: 1Last Post: 04-12-2012, 04:29 AM -
Creating an amount of variables based on user input
By HDInfinity in forum New To JavaReplies: 4Last Post: 04-11-2012, 08:23 AM -
Making a loop and creating variables based off of user input
By seanfmglobal in forum New To JavaReplies: 2Last Post: 01-13-2011, 05:43 AM -
superclass and subclass
By mr idiot in forum New To JavaReplies: 19Last Post: 01-03-2009, 07:29 AM -
which class is superclass and subclass?
By java_fun2007 in forum New To JavaReplies: 0Last Post: 12-11-2007, 08:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks