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
java oop polymorphism
add a comment |
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
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
add a comment |
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
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
java oop polymorphism
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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();
add a comment |
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();
}
add a comment |
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
add a comment |
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();
add a comment |
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();
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();
edited Jan 19 at 12:18
answered Jan 19 at 12:13
YousafYousaf
4,4882823
4,4882823
add a comment |
add a comment |
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();
}
add a comment |
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();
}
add a comment |
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();
}
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();
}
answered Jan 19 at 12:07
StefanStefan
1,34111023
1,34111023
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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