0

I have two files, a Section class, for a specific department in a store, and a Dept class, representing a collection of Section classes. I am having trouble accessing methods of the Section class (like getName() or printNameArrList()) AFTER I add the Section objects to the Dept's sectionArr. The methods seem to work when they are instantiated in main, but I get the "error: cannot find symbol" when I try to access anything with store.sectionArr[0].anything()

Section.java

import java.util.ArrayList;
import java.util.Arrays;

public class Section {
    public static String name;
    public static ArrayList<String> arrList;

    public void setName(String text) {
        name = text;
    }

    public String getName() {
        return name;
    }

    public ArrayList getArrList() {
        return arrList;
    }

    public void printNameArrList() {
        System.out.print("The " + name + " section sells these items: ");
        for (int i = 0; i < arrList.size(); i++) {
            String value = arrList.get(i);
            System.out.print(value + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {

    }

}

Dept.java

import java.util.Arrays;
import java.util.ArrayList;

public class Dept {
    public static String name;
    public Object[] sectionArr;

    public void setName(String text) {
        name = text;
    }

    public String getName() {
        return name;
    }

    public Object[] getObjArr() {
        return sectionArr;
    }

    public void initArr(int size) {
        sectionArr = new Object[size];
    }

    public static void main(String[] args) {
        Dept store = new Dept();

        store.setName("Floyd's Floors");

        System.out.println(store.getName());

        System.out.println(store.getObjArr());

        store.initArr(3);

        System.out.println(store.getObjArr());

        Section sec1 = new Section();

        sec1.setName("Shoes");
        sec1.arrList = new ArrayList<String>();
        sec1.arrList.add("Jordan's");
        sec1.arrList.add("Rodman's");
        sec1.arrList.add("Shaq's");
        sec1.getArrList();
        sec1.printNameArrList();

        store.sectionArr[0] = sec1;

        for (int i = 0; i < store.sectionArr.length; i++) {
            System.out.println(store.sectionArr[i]);
        }

        Object secX = store.sectionArr[0];
        secX.printNameArrList();

    }


}
  • Possible duplicate of [What does a "Cannot find symbol" compilation error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) – MC Emperor Aug 19 '18 at 19:14
  • 1
    That is because you have defined `sectionArr` to be an `Object[]`. So the compiler only knows that there are `Object`s in that array. The compiler just doesn't know that the *actual* type of objects are `Section`s. Of course it is letting you only access the methods of `Object`. You have to tell the compiler that your array contains `Section`s. `Section[] sectionArr` will do fine. – MC Emperor Aug 19 '18 at 19:16
  • 1
    Oh and [instance fields should not be public](https://crunchify.com/java-tips-never-make-an-instance-fields-of-class-public/) in almost all cases. [Encapsulation](https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)) and stuff. – MC Emperor Aug 19 '18 at 19:19

2 Answers2

0

I just inserted it into my IDE, which highlights secX.printNameArrList(); (last statement in Dept.java).

Reason: secX is of type Object, not Section. This has nothing to do with visibility.

sectionArr should probaby be a Section[] instead of an Object[]. I also believe that name and arrList in Section should not be static.

Anyway, here's the updated Dept class, with some improvements:

import java.util.Arrays;
import java.util.ArrayList;

public class Dept {
    public static String name;
    public Section[] sectionArr;

    public void setName(String text) {
        name = text;
    }

    public String getName() {
        return name;
    }

    public Section[] getObjArr() {
        return sectionArr;
    }

    public void initArr(int size) {
        sectionArr = new Section[size];
    }

    public static void main(String[] args) {
        Dept store = new Dept();

        store.setName("Floyd's Floors");

        System.out.println(store.getName());
        System.out.println(Arrays.toString(store.getObjArr()));

        store.initArr(3);

        System.out.println(Arrays.toString(store.getObjArr()));

        Section sec1 = new Section();

        sec1.setName("Shoes");
        sec1.arrList = new ArrayList<>();
        sec1.arrList.add("Jordan's");
        sec1.arrList.add("Rodman's");
        sec1.arrList.add("Shaq's");
        sec1.printNameArrList();

        store.sectionArr[0] = sec1;

        for (int i = 0; i < store.sectionArr.length; i++) {
            System.out.println(store.sectionArr[i]);
        }

        Section secX = store.sectionArr[0];
        secX.printNameArrList();
    }
}

PS: I removed the sec1.getArrList() statement since it does nothing.

Aloso
  • 5,123
  • 4
  • 24
  • 41
0

You can make the following change in the main method in Dept class:

Section secX = store.sectionArr[0];
((Section )secX).printNameArrList();

Object class is parent class of all classes in Java. So due to covarinace you can hold child class object in Parent class reference. But the parent class reference doesn't know about the methods present in child class and thus, you are getting compile error. So to overcome that you can use ((Section )secX).printNameArrList();.

Yug Singh
  • 3,112
  • 5
  • 27
  • 52