简体   繁体   中英

Dr. Java Abstract Animal Class not Compiling - Beginner

abstract class Animal {

  public abstract void sound();

  public abstract void movement();

}



public class Cat extends Animal{

public void sound(){
   System.out.println("Meows");
}

public void movement(){
System.out.println("Pounces");
}

public static void main(String args[]){

 Animal obj = new Cat();
obj.sound();
}

 Animal obj = new Cat();
obj.movement();
}




public class Dolphin extends Animal{

public void sound(){
System.out.println("Whistles");
}

public void movement(){
 System.out.println("Swims");
}

public static void main(String args[]){

 Animal obj = new Dolphin();
 obj.sound();
 }

 Animal obj = new Dolphin();
 obj.movement();
  }





public class Parrot extends Animal{

public void sound(){
System.out.println("Talks");
}

public void movement(){
System.out.println("Flies");
}

 public static void main(String args[]){

 Animal obj = new Parrot();
 obj.sound();
 }

Animal obj = new Parrot();
obj.movement();
  }

It's not compiling? I don't know what's wrong. Sorry I'm a real beginner. I'm supposed to create an abstract animal class with a variable of species and methods sound and movement and three other child classes that inherit from the first one. The child classes must use the sound and movement methods and also have some other methods of their own. Can someone help me? Thanks a lot.

I'm not quite sure why you design your source code like that, try something like this.

The abstract class.

public abstract class Animal {
    public abstract void sound();
    public abstract void movement();
}

The concrete class, I took only Cat class.

public class Cat extends Animal{

    public void sound(){
       System.out.println("Meows");
    }

    public void movement(){
        System.out.println("Pounces");
    }
}

The main class.

public class Main{
   public static void main(String[] args){
       Animal cat = new Cat();
       cat.sound();
       cat.movement();
   }
}

Move your main method into another class, don't put it on the concrete class then try compile the Main class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM