• Post Reply Bookmark Topic Watch Topic
  • New Topic

cannot resolve method  RSS feed

 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I complile the following code, I get a cannot resolve method "doYell{}" in class Animals.
How do I fix this? Is it because of late binding?
interface Yell {
public void doYell();
}
class dog extends Animals implements Yell{
public void doYell() {
System.out.println("bark");
}
}
class cat extends Animals implements Yell{
public void doYell() {
System.out.println("Meow");
}
}
class cow extends Animals implements Yell{
public void doYell() {
System.out.println("Moo");
}
}

public class Animals {
void AnimalSounds(Animals [] someAnimals) {
for (int i = 0;i < someAnimals.length; i++) {
someAnimals[i].doYell();
}
}
public static void main(String [] args) {
Animals a = new Animals();
a.AnimalSounds(new Animals[] {new dog(), new cat(), new cow()});
}
}

P.S.: how do I get my text to allign good when I'm posting. I've been copying from TextPad?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that your Animals class should implement the Yell interface and be decalred abstract like this:

Then there will be no need to delare dog, cat and cow to implement Yell although it won't hurt anything to do so.
 
donald rieck
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you soo much for the hint;however, I had to add a class because a abstract class cannot be instantained.
nterface Yell {
public void doYell();
}
class dog extends Animals implements Yell{
public void doYell() {
System.out.println("bark");
}
}
class cat extends Animals implements Yell{
public void doYell() {
System.out.println("Meow");
}
}
class cow extends Animals implements Yell{
public void doYell() {
System.out.println("Moo");
}
}

abstract class Animals implements Yell{
}

public class MyAnimals {
void AnimalSounds(Animals [] someAnimals) {
for (int i = 0;i < someAnimals.length; i++) {
someAnimals[i].doYell();
}
}
public static void main(String [] args) {
MyAnimals a = new MyAnimals();
a.AnimalSounds(new Animals[] {new dog(), new cat(), new cow()});
}
}
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to add a class because a abstract class cannot be instantained.
Hey, I had to leave some work for you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!