Results 1 to 8 of 8
  1. #1
    mc54321 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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):

    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;
    	}
    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:
    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'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.

    I see that the superclass uses getter/setter methods, but I don't understand how to utilize them in my class.



    Thank you.
    - MC
    Last edited by mc54321; 11-04-2012 at 08:08 PM.

  2. #2
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: How to store user input from subclass into variables from superclass

    you define a subclass, which uses the extends keyword.
    Java Code:
    class SubClass extends Superclass
    the methods which declared public are accesible for the subclass, the private variable isn't.
    you need to use the getter/setter methods to access the private variable.
    Java Code:
    SubClass ins = new SubClass();
    ins.setAnswer(answer);
    which will use the setAnswer method to change the private variable..

  3. #3
    mc54321 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: How to store user input from subclass into variables from superclass

    Using that, I get an error message.

    Java Code:
    ins.setAnswer(answer);
    // The error message says that "answer" is not visible
    I had already had an instantiation written in my program, looking like:
    Java Code:
    Subclass x = new Subclass();
    Should that be kept in? Or does Subclass ins = new Subclass(); do the same thing?

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

    Default 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());

  5. #5
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

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

  6. #6
    mc54321 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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"?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: How to store user input from subclass into variables from superclass

    Quote Originally Posted by mc54321 View Post
    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"?
    Put the user's input into an object of this class first.

  8. #8
    mc54321 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: How to store user input from subclass into variables from superclass

    Thank you.

Similar Threads

  1. Replies: 1
    Last Post: 04-12-2012, 04:29 AM
  2. Creating an amount of variables based on user input
    By HDInfinity in forum New To Java
    Replies: 4
    Last Post: 04-11-2012, 08:23 AM
  3. Replies: 2
    Last Post: 01-13-2011, 05:43 AM
  4. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 07:29 AM
  5. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 08:55 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
  •