0

I am trying to access the super class variable name from user input. I am not sure how to have the super class variable name point to the user input. Here is the code for it. Any ideas thank you.

    package chapter4;
    import java.util.Scanner;

    public class VetOffice extends Animal {

        public VetOffice(int lifeExpectancy, int weight, String name, Character gender, String type) {
            super(lifeExpectancy, weight, name, gender, type);
            // TODO Auto-generated constructor stub
        }
       public static void main(String[] args) {

        Scanner console = new Scanner(System.in);

                System.out.print("Please enter name of pet");
                //super(name);
                //= console.next();
        //}
        }   
    }
    //}
Derrick C
  • 19
  • 1

1 Answers1

0

The thing is you cannot access super class variables in main method. Because it is static method. If you want to access in main() you have to make Animal class name variable to static. Then you can assgin a value directly in main().

Like this:

in Animal class,

static String name;

in VetOffice,

name = console.next();

You can try different ways depending on the thing that you are going to achieve you have to decide,

Is this variable can be declare as static or not? Because static variables common for every object.

Another way you can do this is,

Create getters and setters for Animal class member variables. Then also you cannot access in the main method because also you have to make those methods and variable to static.

As a solution without makinng them static or a new methods even getters and setters in super class you can create default constructor for super class and assign values like below:

If your variable in the super class is private you have to create getters and setters.

public static void main(String[] args) {
   Scanner console = new Scanner(System.in);
   System.out.print("Please enter name of pet");

   VetOffice pet = new VetOffice();
   pet.name = console.next();
   System.out.println(pet.name);
} 

Note: create default constructor If it is unnecessary to create object from VetOffice or super class seeing that you have to pass values to constructor.

UPDATE:

According to your comment

If your variable in the super class is private do this:

public static void main(String[] args) {
   Scanner console = new Scanner(System.in);
   System.out.print("Please enter name of pet");

   VetOffice pet = new VetOffice();
   pet.setName(console.next());
   System.out.println(pet.getName());
}

Another way that you asked for in the comment:

Animal class(partialy implemented to show you)

public class Animal {
    int lifeExpectancy;
    static int weight;
    static String name;

    Animal(int lifeExpectancy, int weight, String name, Character gender, String type){
        this.weight = weight;
        this.name = name;
    }

    public static String getName() {
        return name;
    }
    public static void setName(String n) {
        name = n;
    }
}

Then in the main method:

Animal.setName(console.next());
System.out.println(Animal.getName());
Blasanka
  • 21,001
  • 12
  • 102
  • 104
  • I have getter and setters in the animal class. So how do i create a VetOffice object. How do I call the setName() in VetOffice. – Derrick C May 24 '17 at 01:58
  • Ok I understand that but why would I not call the variable super to access the variable name from the super class. – Derrick C May 24 '17 at 02:05
  • @Derrick C `super()` is not a variable. Read this - https://stackoverflow.com/questions/44134360/inheritance-with-constructor-in-java/44135109#44135109 – Blasanka May 24 '17 at 02:07
  • I tried that but it says to create a VetOffice constructor and when I did that it said. Implicit super constructor animal is undefined must explicitly invoke another constructor. Is there a way I can use setName with the superclass. ie Animal.setName or super(setName). – Derrick C May 24 '17 at 02:29
  • if you want `super(setName)` this has to be in constructor – Blasanka May 24 '17 at 02:31
  • As I said before `Animal.setName` you can do like that if you make setters, getters and `name` variable to static – Blasanka May 24 '17 at 02:33
  • @Derrick C I updated the answer. Look at the bottom of answer in update section. – Blasanka May 24 '17 at 02:37
  • I did that but I get a null value returned when I execute System.out.println(Animal.getName()); – Derrick C May 24 '17 at 02:45
  • @DerricC Change `setName(String n)` and `name = n;` – Blasanka May 24 '17 at 02:50
  • 1
    ok it is working thank you. How can I get a hold of you for future questions? – Derrick C May 24 '17 at 02:58
  • I think there is no way for that but you can mention me in your question comments - @Blasanka – Blasanka May 24 '17 at 03:00