I've just started learning object oriented programming from the book head first java.It said that polymorphism enables me to create an array of the superclass type and then have all the subclasses as the array elements.But when I tried writing code using the same principles it ran into error saying
error: cannot find symbol
I made the classes the superclass was animal and the dog class extended the animal class having a fetch method of its own, but when I referenced the dog variable as animal it did not work here is the code
The Animal class:
public class animal{
String family;
String name;
public void eat() {
System.out.println("Ghap Ghap");
}
public void roam() {
System.out.println("paw paw");
}
}
The dog class:
public class dog extends animal {
public void fetch() {
System.out.println("Auoooooooo");
}
}
The Tester class:
public class tester {
public static void main(String args[]){
animal doggie = new dog();
doggie.fetch();
doggie.eat();
doggie.roam();
}
}
The error:
tester.java:4: error: cannot find symbol
doggie.fetch();
^
symbol: method fetch()
location: variable doggie of type animal
1 error
Edit: Last time I asked this question I went home thinking the object doggie
is of the type animal
and it has no idea of about the fetch()
function that has been declared in the dog
class. But adding the line
System.out.println(doggie.getClass().getName());
Gives dog
as the type of the class, if dog
is indeed the type of the class, shouldn't it have the knowledge of the method declared within it
?
util. List class without declaring the corresponding import, therefore the cannot find symbol error occurs. Adding the missing import statement (line 4 in Fig. 4(b)) solves the problem.
The “cannot find symbol” error comes up mainly when we try to use a variable that's not defined or declared in our program. When our code compiles, the compiler needs to verify all the identifiers we have. The error “cannot find symbol” means we're referring to something that the compiler doesn't know about.
It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading. Note: But Java doesn't support the Operator Overloading.
It means that either there is a problem in your Java source code, or there is a problem in the way that you are compiling it. Your Java source code consists of the following things: Keywords: like class , while , and so on. Literals: like true , false , 42 , 'X' and "Hi mum!" .
When using polymorphism, if you create an instance of the subclass and store its reference in a variable of superclass type, you can only call those methods on the newly created instance which are present in the super class.
In your code, you created an instance of dog
class and stored its reference in doggie
which is of type animal
(super class of dog), In such case, you can't call any method on dog
class instance that isn't available in animal
class.
fetch
method is not defined in the animal
class hence you get the error.
Solution
Either define the fetch
method in the animal
class
OR
change
animal doggie = new dog();
to
dog doggie = new dog();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With