12/13 part 1


#1



https://www.codecademy.com/courses/learn-java/lessons/object-oriented-programming/exercises/inheritance?action=lesson_resume&link_content_target=interstitial_lesson


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


Anyone who can see why?


#2

At first think
where the checkStatus method come from?

did you complete your first instruction?


#3

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


#4

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


#5

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


#6

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:


#7

Yes.
Thank's for all the help.


#8

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