Missing Method body, or declare abstract

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I am getting this error message:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Pet.saySomething at PetTest.main(PetTest.java:18) Java Result: 1

Here is what i have: for the Speak class,

public class Speak {    public void saySomething(); } 

Here is what i have in the PetTest class:

 import java.util.ArrayList;   public class PetTest {       public static void main(String[] args)      {          ArrayList<Pet> myPets = new ArrayList<Pet>();           myPets.add(new Parrot("Boss"));          myPets.add(new Cat("Oreo"));          myPets.add(new Dog("Riley"));           for (Pet p: myPets)          {              p.saySomething();              System.out.println(p);          }      }  } 

I did not post the other 5 classes that go with this program. I know there is something that needs to be fixed in the Speak class, but i am stuck. In the Speak class, on the line that says public void saySomething(); it says Missing Method body, or declare abstract.

EDIT: Here are the other classes:

 public abstract class Pet {       String name;       public Pet(String name){          this.name = name;      }  }   public class Parrot extends Bird {       public Parrot (String name)      {          super(name);      }       @Override      public void saySomething()      {          System.out.println("Pauly want a craker?");      }       @Override      public String toString()      {          return "Hello, I'm " + name + " the parrot.";      }   }    public class Dog extends Pet {       public Dog (String name)      {          super(name);      }       @Override      public void saySomething()      {          System.out.println("Ruff, ruff!");      }       @Override      public String toString()      {          return "Hello, I'm " + name + " the dog.";      }  }    public class Cat extends Pet {       public Cat(String name)      {          super(name);      }       @Override      public void saySomething()      {     System.out.println("Meow.");      }       @Override      public String toString()      {          return "Hello, I'm " + name + " the cat.";      }  }   public class Bird extends Pet {       public Bird(String name)      {          super(name);      }       public void saySomething(){}   } 

回答1:

You have the following options for Speak, take your pick:

1st

public abstract class Speak{     public abstract void saySomething();  } 

2nd

public class Speak{     public void saySomething(){     } } 

3rd

public interface Speak{     public void saySomething(); } 

Edit: First of all, you are not using Speak class anywhere. Second, please add the following method in Pet class:

 public abstract void saySomething(); 


回答2:

If Speak is a concrete class, it must implement all of its methods, so saySomething must have a body.

However, it looks like Speak is more suitable to be an interface. Your Pet class can implement that interface.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!