Task 12/13 Inheritance

I know that I am not the first with this question but I couldn’t find the answer. Stuck on task 12 (inheritance), part 2. And I can not understand why? I checked several times, it seems like everything is OK. Call the checkStatus method inside of main. What’s wrong? Help me please.

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

Dog.java:13: error: cannot find symbol
return age;
^
symbol: variable age
location: class Dog
1 error

```

class Dog extends Animal {
int dogsAge;
public Dog(int age) {
dogsAge = age;
}
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(40);
int spikeAge = spike.getAge();
System.out.println(spikeAge);
spike.checkStatus();
}
}

<do not remove the three backticks above>

age is the name of the parameter to the Dog constructor. You can’t access this parameter in the getAge method.

The instance variable is called dogsAge.

Thank you so much!!)) Now everything is OK. But I don’t understand how the system check the task previously, allow me to do the mistake and go to the next task. By the way, in the instructions to the task 11/13 said: " In between the run and main methods, add a method called getAge that returns age by typing
public int getAge() {
return age;
}"
Is it mistake in the instructions or I again do not understand well?

1 Like

Hmm, your code looks like in the instructions.

I can’t open the code editor on my PC so I can’t try it out myself, sorry.

Anyway thanks. You helped me to go to the next task. Maybe I’ll sort out with that later.

I am confused. Here’s my code:

class Dog extends Animal{
2

3
int age;
4

5
public Dog(int dogsAge) {
6

7
age = dogsAge;
8

9
}
10

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

14
public void run(int feet){
15

16
System.out.println(“Your dog ran " + feet + " feet!”);
17

18
}
19

20
public int getAge(){
21

22
return age;
23

24
}
25

26
public static void main(String args) {
27

28
class Vehicle {
29

30
public void checkBatteryStatus() {
31

32
spike.checkStatus();
33

34
Dog spike = new Dog(5);
35

36
spike.bark();
37

38
spike.run(345);
39

40
int spikeAge = spike.getAge();
41

42
System.out.println(spikeAge);
43
}
44

45
}
46

Did you call the checkStatus method inside of main?
Run
Dog.java:11: error: illegal start of type
public void bark({
^
Dog.java:11: error: ‘)’ expected
public void bark({
^
Dog.java:12: error: ‘;’ expected
System.out.println(“Woof!”);}
^
Dog.java:12: error: expected
System.out.println(“Woof!”);}
^
Dog.java:12: error: illegal start of type
System.out.println(“Woof!”);}
^
Dog.java:14: error: class, interface, or enum expected
public void run(int feet){
^
Dog.java:18: error: class, interface, or enum expected
}
^
Dog.java:20: error: class, interface, or enum expected
public int getAge(){
^
Dog.java:24: error: class, interface, or enum expected
}
^
Dog.java:26: error: class, interface, or enum expected
public static void main(String args) {
^
10 errors

Sorry about that. here’s the code:
class Dog extends Animal{

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) {

class Vehicle {

public void checkBatteryStatus() {

spike.checkStatus();

Dog spike = new Dog(5);

spike.bark();

spike.run(345);

int spikeAge = spike.getAge();

System.out.println(spikeAge);
}

}

Here’s the errors:

Dog.java:11: error: illegal start of type
public void bark({
^
Dog.java:11: error: ‘)’ expected
public void bark({
^
Dog.java:12: error: ‘;’ expected
System.out.println(“Woof!”);}
^
Dog.java:12: error: expected
System.out.println(“Woof!”);}
^
Dog.java:12: error: illegal start of type
System.out.println(“Woof!”);}
^
Dog.java:14: error: class, interface, or enum expected
public void run(int feet){
^
Dog.java:18: error: class, interface, or enum expected
}
^
Dog.java:20: error: class, interface, or enum expected
public int getAge(){
^
Dog.java:24: error: class, interface, or enum expected
}
^
Dog.java:26: error: class, interface, or enum expected
public static void main(String args) {
^
10 errors

I messed up again. Here’s my code;
class Dog extends Animal{

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) {

spike.checkStatus();

Dog spike = new Dog(5);

spike.bark();

spike.run(345);

int spikeAge = spike.getAge();

System.out.println(spikeAge);
}

}

Here’s my errors:
Dog.java:11: error: illegal start of type
public void bark({
^
Dog.java:11: error: ‘)’ expected
public void bark({
^
Dog.java:12: error: ‘;’ expected
System.out.println(“Woof!”);}
^
Dog.java:12: error: expected
System.out.println(“Woof!”);}
^
Dog.java:12: error: illegal start of type
System.out.println(“Woof!”);}
^
Dog.java:14: error: class, interface, or enum expected
public void run(int feet){
^
Dog.java:18: error: class, interface, or enum expected
}
^
Dog.java:20: error: class, interface, or enum expected
public int getAge(){
^
Dog.java:24: error: class, interface, or enum expected
}
^
Dog.java:26: error: class, interface, or enum expected
public static void main(String args) {
^
Dog.java:30: error: class, interface, or enum expected
Dog spike = new Dog(5);
^
Dog.java:32: error: class, interface, or enum expected
spike.bark();
^
Dog.java:34: error: class, interface, or enum expected
spike.run(345);
^
Dog.java:36: error: class, interface, or enum expected
int spikeAge = spike.getAge();
^
Dog.java:38: error: class, interface, or enum expected
System.out.println(spikeAge);
^
Dog.java:39: error: class, interface, or enum expected
}
^
16 errors

here you didn’t close the brackets

And try to do as I was advised. [quote=“eveat, post:2, topic:59265, full:true”]
age is the name of the parameter to the Dog constructor. You can’t access this parameter in the getAge method.

The instance variable is called dogsAge.
[/quote]

This is my code now

1 Like

Thanks for helping. I really appreciate it.

hi can you help me with mine please.I cant see where i am going wrong

class Dog {
class Dog extends Animal {

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(40);
int spikeAge = spike.getAge();
System.out.println(spikeAge);
spike.checkStatus();

}

}

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