0

So here is a class:

public class Palya {
    Mezo[][] m;

    public Palya(Mezo[][] m){
        this.m = m;
}

And it's derived:

public class PalyaTeszt extends Palya{

    public PalyaTeszt(Mezo[][] m) {
        super(new Mezo[][]  {
                {new Szikla(),    new Szikla(),     new Viz(),        new Szikla(),     new Szikla(),     new Preri(true)},
                {new Szikla(),    new Szikla(),     new Preri(false), new Preri(false), new Preri(false), new Szikla()},
                {new Szikla(),    new Szikla(),     new Preri(false), new Preri(false), new Szikla(),     new Szikla()},
                {new Szikla(),    new Szikla(),     new Preri(false), new Szikla(),     new Viz(),        new Preri(false)},
                {new Szikla(),    new Szikla(),     new Preri(false), new Szikla(),     new Viz(),        new Szikla()},
                {new Preri(true), new Preri(false), new Preri(false), new Preri(false), new Preri(false), new Preri(false)}
        });
    }
}

In the derived constructor the array is initialized with an other class called Mezo, this Mezo[][] m class has methods that i would like to access to, but there is no name for it, like m.getSomething(). How can this be initialized in the way that i can use the Mezo's methods? Would it be better if the Palya was just an interface? The meaning of Palya is a 6x6 fields like chess board just for different purpose. I would like to have 6 different Palya.

J.Doe
  • 301
  • 1
  • 3
  • 12
  • It sounds like you need to read about the different access level that Java provides. They are `public`, `private`, default (no-specifier), and `protected`. You should also learn about Object Oriented principles such as data hiding. For example, you can create methods in the `Palya` class which in turn call methods on the `Mezo` objects in the array. – Code-Apprentice Apr 02 '18 at 17:26
  • Your code is actually making a **Two-Dimensional Arrays**. You can access to method by getting separate items. For example: m[0][0] which will return the first class in the first array. Here a Szikla class. – 0ddlyoko Apr 02 '18 at 17:26
  • If `PalyaTeszt` and `Palya` are in the same package, you can do `m.doSomething()` just fine. – Code-Apprentice Apr 02 '18 at 17:28
  • Caution: Your `public PalyaTeszt(Mezo[][] m)` constructor never uses its `m` argument. – VGR Apr 02 '18 at 17:33

1 Answers1

1

Problem is that you didn't put access level modifier for your m field in class Palya, it should look like this:

public class Palya {
    protected Mezo[][] m; // Note protected keyword

    public Palya(Mezo[][] m){
        this.m = m;
}

If no modifier is proveded there is default which allows access only inside that class and members of same package.

More about access modifiers in Java - official docs

FilipRistic
  • 2,661
  • 4
  • 22
  • 31