12. Inheritance, extends (71%), code error?


#1

the second part of the extends exercise:
"Within the main method of the Dog class, call the checkStatus method on the spike object."

it is not ok to add the " spike.checkStatus();" at the beginning of main, like this:

public static void main(String[] args) {
spike.checkStatus();
Dog spike = new Dog(5);
spike.bark();
spike.run(40);
int spikeAge = spike.getAge();
System.out.println(spikeAge);

But it is ok if it's typed below the last line (below println). Why?


#2

Here is what you have shown us:

spike.checkStatus();
Dog spike = new Dog(5);

You are asking to check spike's status but you haven't even created spike yet.


#3

spike is an instance of Dog class. You have to create the spike instance first before you can use the new object spike with the classes methods.
You don't have to use it after the println statement you have in your code. You can use it anywhere after your object declaration.


#4

Thanks!
So it goes in a logical order.
I thought it loaded and compiled the whole class at the same time.


#5

A post was split to a new topic: This is still wrong and I can't go to next course


#6