12/13 part 1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-java/lessons/object-oriented-programming/exercises/inheritance?action=lesson_resume&link_content_target=interstitial_lesson

<In what way does your code behave incorrectly? Include ALL error messages.>
I gets an error write the dot in the outprintline, and i don’t understand why:
Dog.java:28: error: cannot find symbol
spike.checkStatus();
^
symbol: method checkStatus()
location: variable spike of type Dog
1 error

```

class Dog {

int age;

public Dog(int dogsAge) {
age = dogsAge;

}

public void bark() {
System.out.println(“Woof!”);
}

public void run(int feet) {
System.out.println(“Your dog ran " + feet + " feet!”);
}

public int getAge() {
return age;
}

public static void main(String[] args) {

Dog spike = new Dog(5);
spike.bark();
spike.run(10);
int spikeAge = spike.getAge();
System.out.println(spikeAge);
spike.checkStatus();

}

}

<do not remove the three backticks above>
Anyone who can see why?

At first think
where the checkStatus method come from?

did you complete your first instruction?

No, I did not.

The task saes:
Note that there are now two files in the code editor. Within the Dog class, use the extends keyword to inherit from the Animal class.

I assumed the task ment I should use checkStatus from Animal.java…

I assumed the task ment I should use checkStatus from Animal.java…

yes :slight_smile:

use extends keyword

you can see the example what you are given

In the eksample, it’s used: myFastCar.checkBatteryStatus.
myFastCar = spike,
and
checkBatteryStatus = checkStatus…

i m talking about extends keyword.
In the example above, the extends keyword is used to indicate that the Car class inherits the behavior defined in the Vehicle class. This makes sense, since a car is a type of vehicle.

now make sense :slight_smile:

there are now two files in the code editor.
Dog.java and Animal.java

click on Animal.java
you’ll see Animal class and checkStatus method.

you are caling checkStatus method within the Dog class

spike.checkStatus();

but how it’ll work if you dont inherit from Animal class because the method you are calling there is no existence within Dog class.

that’s why you have to use extends keyword to inherit from the Animal class.

Hope you’ve understand :slight_smile:

Yes.
Thank’s for all the help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.