Java - Accessing a subclass constructor in a different class

broncosaurus

I am trying to instantiate an object (created in a "EmptySea" subclass of "Ship" class) into another "Ocean" class to fill an array with "EmptySea" objects.

Error is "EmptySea cannot be resolved to a type."

Here is my Ocean class code:

public class Ocean {

    // Instance variables.
    public Ship[][] ships = new Ship[10][10];
    public int shotsFired;
    public int hitCount;

    // Constructor.
    public Ocean() {
        shotsFired = 0;
        hitCount = 0;
        for (int row = 0; row < ships.length; row++) {
            for (int column = 0; column < ships[row].length; column++) {
                ships[row][column] = new EmptySea();

public abstract class Ship {

    // Instance variables.
    private int bowRow;
    private int bowColumn;
    private int length;
    private boolean horizontal;
    private boolean[] hit = new boolean[4];

    // No constructor needed for Ship class.

    // Methods (too many to show).

    public class EmptySea extends Ship {

        // Constructor.
        EmptySea() {
            length = 1;
        }

        // Inherited methods to define.
        int getLength() {
            return length = 1;
        }

        String getShipType() {
            return "Empty";
        }

        @Override
        boolean shootAt(int row, int column) {
            return false;
        }

        @Override
        boolean isSunk() {
            return false;
        }

        @Override
        public String toString() {
            return "-";
        }
    }

The ships array has been properly declared as an instance variable in Ocean class. Basically, it is not letting me put the EmptySea() object (the code for the "Ship" class and its "EmptySea" subclass runs correctly).

Do I need to somehow reference the superclass in this case?

If there's an easier way to do it, I can't do it that way (this way is specified in assignment).

Adrian Colomitchi

Learn about the difference between a static nested class and an instance nested class.

Some other SO question on the same.

Short term: declare your inner EmptySea class with static, then read/understand why - in brief, without static an EmptySea instance cannot be created outside the context of a Ship instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java constructor of a subclass of an abstract class

From Dev

Java: Extending a class, constructor of subclass gives error

From Dev

Java: Extending a class, constructor of subclass gives error

From Dev

Accessing a file in a different Java class?

From Dev

Accessing a file in a different Java class?

From Dev

Different constructor for subclass

From Dev

Accessing constructor properties in class

From Dev

Super constructor in a subclass - Java

From Dev

Super constructor in a subclass - Java

From Java

Problem at calling a constructor if the java class is in a different path

From Dev

Why is it mandatory to call a Super class's parameterized constructor inside subclass's parameterized constructor in java?

From Dev

php: Accessing class variables in constructor

From Dev

Java subclass constructor inherited member

From Dev

Java subclass constructor inherited member

From Dev

Accessing a subclass property from a super class in Swift

From Dev

Accessing a property of a subclass with an array of the base class

From Dev

Accessing a subclass property from a super class in Swift

From Dev

Instantiating a class within a subclass and accessing its instance within another subclass

From Dev

Java Atribute In Class and Subclass

From Dev

Reason for calling super class constructor from subclass?

From Dev

Access subclass' property from base class' constructor

From Dev

Calling super class constructor from subclass

From Dev

Specifying which subclass in super class constructor

From Dev

Inherited class with different constructor

From Dev

java quiz only accessing constructor

From Dev

Accessing variables in public constructor in java

From Dev

Accessing a HashMap from a different class

From Dev

different signatures for accessing class methods

From Dev

Accessing method of different class in jsp

Related Related

  1. 1

    Java constructor of a subclass of an abstract class

  2. 2

    Java: Extending a class, constructor of subclass gives error

  3. 3

    Java: Extending a class, constructor of subclass gives error

  4. 4

    Accessing a file in a different Java class?

  5. 5

    Accessing a file in a different Java class?

  6. 6

    Different constructor for subclass

  7. 7

    Accessing constructor properties in class

  8. 8

    Super constructor in a subclass - Java

  9. 9

    Super constructor in a subclass - Java

  10. 10

    Problem at calling a constructor if the java class is in a different path

  11. 11

    Why is it mandatory to call a Super class's parameterized constructor inside subclass's parameterized constructor in java?

  12. 12

    php: Accessing class variables in constructor

  13. 13

    Java subclass constructor inherited member

  14. 14

    Java subclass constructor inherited member

  15. 15

    Accessing a subclass property from a super class in Swift

  16. 16

    Accessing a property of a subclass with an array of the base class

  17. 17

    Accessing a subclass property from a super class in Swift

  18. 18

    Instantiating a class within a subclass and accessing its instance within another subclass

  19. 19

    Java Atribute In Class and Subclass

  20. 20

    Reason for calling super class constructor from subclass?

  21. 21

    Access subclass' property from base class' constructor

  22. 22

    Calling super class constructor from subclass

  23. 23

    Specifying which subclass in super class constructor

  24. 24

    Inherited class with different constructor

  25. 25

    java quiz only accessing constructor

  26. 26

    Accessing variables in public constructor in java

  27. 27

    Accessing a HashMap from a different class

  28. 28

    different signatures for accessing class methods

  29. 29

    Accessing method of different class in jsp

HotTag

Archive