0

Really new to java here. I am trying to build on an abstract class, Animals, extending it to a subclass Ducks. I'll then create an object ducksworth, that proves Duck inherited the methods from the superclass animals, and can call on references to the abstract class variables. When I try and run my code in Netbeans, it is unable to find a main method call, even though the class AllAnimals contains a main call. Any help would be appreciated? This code is entirely written in a single file in Netbeans.

Ideally what I would like as output is "feather"

abstract class Animal {

String FUR= "fur";
String FEATHER = "feather";
String SCALE = "scale";
String SHELL = "shell";
String SKIN = "skin";

abstract String getCovering( );
abstract void makeSound();
abstract boolean canFly( );

}

class Duck extends Animal {
    String covering  = FEATHER;
    String sound = "quack";
    boolean flight = true;
    String getCovering() {
        System.out.println("Ducks are covered in " + covering);
        return covering;
    }
    void makeSound() { 
        System.out.println(sound);
    }
    boolean canFly() { 
        return flight; 
    }
}

class allanimals {
    public static void main(String[] args) {
        Duck ducksworth = new Duck();
        ducksworth.getCovering();
    }
}
Steve C
  • 29
  • 2
  • What did you name your file and make allanimals class public. Name the file same allanimals. And a bit of a convention, name your class with first letter as capital. – Tahir Hussain Mir Mar 02 '20 at 02:18
  • "When I try and run my code in Netbeans, it is unable to find a main method call" can we see how are you trying to run your project and what is its structure? – Pshemo Mar 02 '20 at 02:52
  • Your file name should be 'allanimals.java' – Anuradha Mar 02 '20 at 06:19

4 Answers4

1

As everyone said. Putting actual class below.

Changes done:

  1. class name allanimals changed to AllAnimals for better standard.
  2. class AllAnimals is made public for jvm to access static main method.
  3. File name is given as public class name - AllAnimals.java

File name : AllAnimals.java

abstract class Animal {

String FUR= "fur";
String FEATHER = "feather";
String SCALE = "scale";
String SHELL = "shell";
String SKIN = "skin";

abstract String getCovering( );
abstract void makeSound();
abstract boolean canFly( );

}

class Duck extends Animal {
    String covering  = FEATHER;
    String sound = "quack";
    boolean flight = true;
    String getCovering() {
        System.out.println("Ducks are covered in " + covering);
        return covering;
    }
    void makeSound() { 
        System.out.println(sound);
    }
    boolean canFly() { 
        return flight; 
    }
}

public class AllAnimals {
    public static void main(String[] args) {
        Duck ducksworth = new Duck();
        ducksworth.getCovering();
    }
}

Jags
  • 799
  • 7
  • 19
  • Can you explain your answer a little? What changes did you made and why (what problem OP is facing and why those changes solve it)? – Pshemo Mar 02 '20 at 15:14
  • Point (2) doesn't look right since non-public types (classes, interfaces, enums) can also contain `main` method so JVM doesn't use that information to determine its location. – Pshemo Mar 02 '20 at 18:05
0

How are you running the project?

It's simplest to have each class in a file so you could simply compile and run the class with your main https://www.protechtraining.com/content/java_fundamentals_tutorial-hello_world

Alternatively, take a look here: Java: Multiple class declarations in one file

https://www.quora.com/Can-we-keep-more-than-one-class-in-a-single-java-file

0

This is not the JDK 8 idiom I'd recommend. Prefer an interface to an abstract class. Add default methods where appropriate.

When will we see the end of Animal as the go-to inheritance example? Not today, apparently.

Here's how I might do it:

public interface Animal {
    String getCovering();
    void makeSound();
    boolean canFly();
}


public class Duck implements Animal {    
    public String getCovering() { return "feathers"; }
    public void makeSound() { return "quack"; }
    boolean canFly() { return true; } 
}

It's not too soon to learn about JUnit:

public class AnimalTest {

    @Test
    public void testList() {
        // setup
        List<Animal> animals = Collections.singletonList(new Duck());
        List<String> expectedCoverings = Collections.singletonList("feathers");
        List<String> expectedSounds = Collections.singletonList("quack");
        List<Boolean> expectedFly = Collections.singletonList(Boolean.TRUE);
        // exercise
        // assert
        for (int i = 0; i < animals.size(); ++i) {
            Assert.assertEquals(expectedCovering.get(i), animals.get(i).getCovering());
            Assert.assertEquals(expectedSound.get(i), animals.get(i).makeSound());
            Assert.assertEquals(expectedFly.get(i), animals.get(i).canFly());
        }
    }
}

The test is easy to extend as you add more Animal implementations.

duffymo
  • 305,152
  • 44
  • 369
  • 561
-1

At least one class in file should be the public one, in this case is preferable that the file get galled allanimals.java and this class must be marked as the public one.

Sombriks
  • 3,370
  • 4
  • 34
  • 54
  • 1
    There is no requirement that at least one class in a .java file needs to be public. – whaley Mar 02 '20 at 02:38
  • 1
    Compiler is forcing requirement that: *if we are declaring top level public class in source `.java` file then that class needs to have same name as source file name* meaning that if we write `public class ...{..}` inside `Foo.java` then only valid name for it is `public class Foo {..}`. But that **doesn't** mean that `Foo` class must be `public`. We can also have `class Foo{..}` inside `Foo.java`. Also there is no requirement from `main` method to be only placed in `public` class. – Pshemo Mar 02 '20 at 02:49