Mrak Vladar
Mrak Vladar

Reputation: 608

Polymorphism in Java error:cannot find Symbol

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 ?

Upvotes: 5

Views: 1669

Answers (4)

Giorgi Tsiklauri
Giorgi Tsiklauri

Reputation: 11136

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.

Upvotes: 1

Yousaf
Yousaf

Reputation: 29344

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

Upvotes: 7

Ratish Bansal
Ratish Bansal

Reputation: 2462

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.

Upvotes: 4

Stefan
Stefan

Reputation: 2188

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

Upvotes: 3

Related Questions