Polymorphism in Java error:cannot find Symbol












4















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









share|improve this question

























  • Can you add the full error log so it is easier to understand ?

    – Koralp Catalsakal
    Jan 19 at 12:04











  • Please start your Java classes with capital case.

    – Stefan
    Jan 19 at 12:04
















4















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









share|improve this question

























  • Can you add the full error log so it is easier to understand ?

    – Koralp Catalsakal
    Jan 19 at 12:04











  • Please start your Java classes with capital case.

    – Stefan
    Jan 19 at 12:04














4












4








4


0






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









share|improve this question
















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






java oop polymorphism






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 12:08







Mrak Vladar

















asked Jan 19 at 12:00









Mrak VladarMrak Vladar

424




424













  • Can you add the full error log so it is easier to understand ?

    – Koralp Catalsakal
    Jan 19 at 12:04











  • Please start your Java classes with capital case.

    – Stefan
    Jan 19 at 12:04



















  • Can you add the full error log so it is easier to understand ?

    – Koralp Catalsakal
    Jan 19 at 12:04











  • Please start your Java classes with capital case.

    – Stefan
    Jan 19 at 12:04

















Can you add the full error log so it is easier to understand ?

– Koralp Catalsakal
Jan 19 at 12:04





Can you add the full error log so it is easier to understand ?

– Koralp Catalsakal
Jan 19 at 12:04













Please start your Java classes with capital case.

– Stefan
Jan 19 at 12:04





Please start your Java classes with capital case.

– Stefan
Jan 19 at 12:04












3 Answers
3






active

oldest

votes


















6














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





share|improve this answer

































    2














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





    share|improve this answer































      2














      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.






      share|improve this answer
























      • 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?

        – Mrak Vladar
        Jan 19 at 12:18











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

        – Ratish Bansal
        Jan 19 at 12:22













      • alright Thanks,I did think of it myself on it being a bad design, I will surely look into instanceOf, thanks!!

        – Mrak Vladar
        Jan 19 at 12:26











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266858%2fpolymorphism-in-java-errorcannot-find-symbol%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      6














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





      share|improve this answer






























        6














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





        share|improve this answer




























          6












          6








          6







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





          share|improve this answer















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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 19 at 12:18

























          answered Jan 19 at 12:13









          YousafYousaf

          4,4882823




          4,4882823

























              2














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





              share|improve this answer




























                2














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





                share|improve this answer


























                  2












                  2








                  2







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





                  share|improve this answer













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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 19 at 12:07









                  StefanStefan

                  1,34111023




                  1,34111023























                      2














                      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.






                      share|improve this answer
























                      • 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?

                        – Mrak Vladar
                        Jan 19 at 12:18











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

                        – Ratish Bansal
                        Jan 19 at 12:22













                      • alright Thanks,I did think of it myself on it being a bad design, I will surely look into instanceOf, thanks!!

                        – Mrak Vladar
                        Jan 19 at 12:26
















                      2














                      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.






                      share|improve this answer
























                      • 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?

                        – Mrak Vladar
                        Jan 19 at 12:18











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

                        – Ratish Bansal
                        Jan 19 at 12:22













                      • alright Thanks,I did think of it myself on it being a bad design, I will surely look into instanceOf, thanks!!

                        – Mrak Vladar
                        Jan 19 at 12:26














                      2












                      2








                      2







                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 19 at 12:10









                      Ratish BansalRatish Bansal

                      2539




                      2539













                      • 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?

                        – Mrak Vladar
                        Jan 19 at 12:18











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

                        – Ratish Bansal
                        Jan 19 at 12:22













                      • alright Thanks,I did think of it myself on it being a bad design, I will surely look into instanceOf, thanks!!

                        – Mrak Vladar
                        Jan 19 at 12:26



















                      • 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?

                        – Mrak Vladar
                        Jan 19 at 12:18











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

                        – Ratish Bansal
                        Jan 19 at 12:22













                      • alright Thanks,I did think of it myself on it being a bad design, I will surely look into instanceOf, thanks!!

                        – Mrak Vladar
                        Jan 19 at 12:26

















                      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?

                      – Mrak Vladar
                      Jan 19 at 12:18





                      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?

                      – Mrak Vladar
                      Jan 19 at 12:18













                      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.

                      – Ratish Bansal
                      Jan 19 at 12:22







                      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.

                      – Ratish Bansal
                      Jan 19 at 12:22















                      alright Thanks,I did think of it myself on it being a bad design, I will surely look into instanceOf, thanks!!

                      – Mrak Vladar
                      Jan 19 at 12:26





                      alright Thanks,I did think of it myself on it being a bad design, I will surely look into instanceOf, thanks!!

                      – Mrak Vladar
                      Jan 19 at 12:26


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266858%2fpolymorphism-in-java-errorcannot-find-symbol%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Homophylophilia

                      Updating UILabel text programmatically using a function

                      Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage