Polymorphism in Java error:cannot find Symbol
Asked Answered
R

4

5

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 ?

Raymundorayna answered 19/1, 2019 at 12:0 Comment(2)
Can you add the full error log so it is easier to understand ?Ctenoid
Please start your Java classes with capital case.Handfasting
B
7

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();
Bartley answered 19/1, 2019 at 12:13 Comment(0)
P
4

Since the fetch() method doesn't exist in animal, its throwing the error. You can define a fetch method in animal and override it in dog class.

Philomena answered 19/1, 2019 at 12:10 Comment(3)
okay, but If I'm designing a game and it has a bunch of animals in it, so for instance if a particular as something unique , like fetch for the dog although other animals don't have this method I will still have to add this method in the super class and then over ride it. right?Raymundorayna
Animal should only contains methods that are common to all animals,like makeSound(), noOfLegs() etc. and each derived class can override the method with its own implementation. You should not add implementation specific methods to animal class,just for the sake of calling via animal reference,that is bad design. You should make use of instanceOf for calling implementation specific methods.Philomena
alright Thanks,I did think of it myself on it being a bad design, I will surely look into instanceOf, thanks!!Raymundorayna
H
3

You are referencing doggie.fetch() but this is not a method defined in animal.

Since you are using your doggie object as an animal you can not use this method.

If you would like to use the method, you can do something like an instance check:

 if(doggie instanceOf dog){
    ((dog)doggie).fetch();
 }
Handfasting answered 19/1, 2019 at 12:7 Comment(0)
A
1

If you really want to understand the depth of this concept, you should understand the Liskov Substitution Principle, which, in a brief, is described as follows:

In a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., an object of type T may be substituted with any object of a subtype S) without altering any of the desirable properties of the program.

Central idea behind this concept is to NOT break the contract "signed" with the parent type (that is, extending a class, or implementing an interface).

If you were able to invoke any method, available in your subtype, on the reference stored in its parent type, disregarding the contract between your subtype and its super type(s), you may have an unintended malfunction - runtime exceptions, to be more precise.

Last but not least: Please follow the Java naming conventions and name your classes with the Pascal Case convention. This is very important.

Avilla answered 15/2, 2021 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.