-1

So as I understand, Goat class has no problem with a code, I want to assign the first two elements from a new object, but I am not sure why it gives an runtime error

class Pet {
    public String name;
    public boolean indoor;

    public Pet(String name, Boolean indoor) {
        this.name = name;
        this.indoor = indoor;
    }


    public String toString(){
        return name + ", " + indoor; 
    }
}

class Goat extends Pet {
    public int age;
    public String diet;
    public Goat(String name, boolean indoor, int age, String diet) {
         super(name, indoor);
         this.age = age;
         this.diet = diet;
    }

Here is the test code and error

test code & error}

G.Lee159
  • 39
  • 5
  • your ```public Pet(String name, Boolean indoor)``` takes a Boolean object, instead of boolean . See https://stackoverflow.com/questions/3728616/boolean-vs-boolean-in-java – luksch Apr 07 '18 at 07:05
  • You seem to have compiled Goat against the Pet class you're showing us, but to be running the test with a different version of the Pet class. – JB Nizet Apr 07 '18 at 07:10
  • this code runs fine... can you provide the test code – shahaf Apr 07 '18 at 07:16

1 Answers1

-1

The error is being thrown because, Pet class constructor is expecting Boolean and Goat class constructor is passing boolean. Although compiler is compiling code, but at runtime java is not able to find boolean type constructor. Hence, throwing no method error.

Correct Code will be

class Pet {
    public String name;
    public boolean indoor;

    public Pet(String name, boolean indoor) {
        this.name = name;
        this.indoor = indoor;
    }


    public String toString(){
        return name + ", " + indoor; 
    }
}

class Goat extends Pet {
    public int age;
    public String diet;
    public Goat(String name, boolean indoor, int age, String diet) {
         super(name, indoor);
         this.age = age;
         this.diet = diet;
    }
Nitishkumar Singh
  • 1,781
  • 1
  • 15
  • 33