-3

here is my source code i named my Project as Animal but when I run it. There is an error locating my MAIN CLASS. Please help me. I am still a beginner and i am open to learn a lot more.

package animal;

public class Animal {  
    public Animal() {
        System.out.println("A new animal has been created!");
    }

    public void sleep() { 
        System.out.println("An animal sleeps...");
    }

    public void eat() {
        System.out.println("An animal eats...");
    }
}

public class Bird extends Animal {
    public Bird() {
        super();
        System.out.println("A new bird has been created!");
    }

    @Override
    public void sleep() {
        System.out.println("A bird sleeps...");
    }

    @Override
    public void eat() {
        System.out.println("A bird eats...");
    }
}

public class Dog extends Animal {
    public Dog() {
        super();
        System.out.println("A new dog has been created!");
    }

    @Override
    public void sleep() {
        System.out.println("A dog sleeps...");
    }

    @Override
    public void eat() {
        System.out.println("A dog eats...");
    }
}

public class MainClass {
    public static void main(String[] args) {
        Animal animal = new Animal();
        Bird bird = new Bird();
        Dog dog = new Dog();
        System.out.println();

        animal.sleep();
        animal.eat();
        bird.sleep();
        bird.eat();
        dog.sleep();
        dog.eat();
    }
}

Please help me with this.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
sawpaw300
  • 3
  • 1
  • 3
    Please post the full and complete error message. And your question is confusing in that you have an error, and yet you state it is error free. It can't be error free, else you wouldn't be here asking why it's not working. – Hovercraft Full Of Eels Feb 11 '17 at 02:24
  • Also, your code formatting is not good, especially your code indentation. Understand that code formatting isn't there to make code "look good" but rather the rules are there to help you quickly see what code belongs to what scope, something that helps you debug and understand your code. You will want to put in the effort to format well, for *your* benefit, so you can more easily debug problems, and for *ours* so we can more easily understand your code and help you. This isn't a trivial request. – Hovercraft Full Of Eels Feb 11 '17 at 02:25
  • How do you run your code? – Tom Feb 11 '17 at 02:34
  • @HovercraftFullOfEels I wonder about the 'inheritance' tag, is it important for this question? It uses inheritance, yes, but does that matter? – Tom Feb 11 '17 at 02:36

3 Answers3

0

Ideally, you should have Bird, Dog, and Animal in their own files on the same directory. Then you create one more class, a tester class, that has a main function, and since it's in the same directory as Bird, Dog, and Animal, it can access them and you can do what your main class does now. If you NEED to have it all in one file, make every class header just class Bird {, etc. Then rename the file to MainClass.java and it'll run.

MacStation
  • 411
  • 4
  • 20
0

Please refer: Can a java file have more than one class?

For short: there can only be one public top-level class per .java file.

So, you should edit your code:

package Animal;

class Animal {
    public Animal() {
        System.out.println("A new animal has been created!");
    }

    public void sleep() { 
        System.out.println("An animal sleeps...");
    }

    public void eat() {
        System.out.println("An animal eats...");
    }
}

class Bird extends Animal {
    public Bird() {
        super();
        System.out.println("A new bird has been created!");
    }

and so on ...

Community
  • 1
  • 1
kitleong
  • 1
  • 2
0

As studied your code,found the error only on class modifier(public) you declared.

" you can have only one public class modifier in a java source file"

We can declare one class in a single source file with these constraints:

  1. Each source file should contain only one public class and the name of that public class should be similar to the name of the source file.
  2. If you are declaring a main method in your source file then main should lie in that public class.
  3. If there is no public class in the source file then main method can lie in any class and we can give any name to the source file.

So.. Your code is..

   class Animal {  
        public Animal() {
    System.out.println("A new animal has been created!");
   }
   public void sleep() { 
    System.out.println("An animal sleeps...");
  }
  public void eat() {
    System.out.println("An animal eats..."); }
  }
  class Bird extends Animal {
   public Bird() {
    super();
    System.out.println("A new bird has been created!");
   }
   @Override
   public void sleep() {
    System.out.println("A bird sleeps...");
   }
    @Override
    public void eat() {
    System.out.println("A bird eats...");
    }
    }
    class Dog extends Animal {
     public Dog() {
    super();
    System.out.println("A new dog has been created!");
    }
     @Override
     public void sleep() {
    System.out.println("A dog sleeps...");
    }
     @Override
     public void eat() {
     System.out.println("A dog eats...");
       }
      }
     public class MainClass {
     public static void main(String[] args) {
      Animal animal = new Animal();
      Bird bird = new Bird();
      Dog dog = new Dog();
      System.out.println();

    animal.sleep();
    animal.eat();
    bird.sleep();
    bird.eat();
    dog.sleep();
    dog.eat();}
    }
Community
  • 1
  • 1
Prem Murmu
  • 116
  • 1
  • 10