0

My main method in a package by itself can only see some of the methods in the other package even though they are all public. This is an example of my code.

    package stuff.code;
    public class AObject
    {
        public AObject();
        public AObject(String str);
        public int getLength();
        public int getHeight();
    }

    package stuff.code;
    public class BObject extends AObject
    {
        public BObject();
        public BObject(String str);
    }

    package stuff.test;
    import stuff.code.AObject;
    import stuff.code.BObject;
    public class tester
    {
        AObject a = new AObject(); //no red underline
        AObject aa = new AObject("some string"); //no red underline
        BObject b = new BObject(); //no red underline
        BObject bb = new BObject("some string"); //tells me there is no such constructor
        b.getLength(); //tells me I need a getLine method in BObject too
    }

I've looked into setAccessible(boolean flag) method but I didn't understand most of it and it was still undefined after I imported java.lang.reflect.AccessibleObject.

This is not my actual code. I wanted this to be vague so that an answer would be more useful to many other people that run into this problem. My methods are all concrete with a body but the body isn't the issue. It runs fine when everything is in the same package but the tester class must be in a different package.

D.B
  • 33
  • 7
  • 1
    Is this your real code? Methods without body should only work in abstract classes or interfaces, not in normal classes like AObject and BObject. Could it be that the classes were not compiled anymore because of these problems and that in tester you are still working with earler versions of theses classes? – Florian S. Jul 02 '17 at 16:40
  • 1
    How can it "work fine" if it does not even compile ? – c0der Jul 02 '17 at 16:43
  • Good point. I changed it – D.B Jul 02 '17 at 16:46
  • Do a clean rebuild, throwing away any old compile product. (Assuming your original code has `{ ... }` instead of `;`.) Seems eclipse has a hickup. – Joop Eggen Jul 02 '17 at 16:50
  • If all methods and constructors have bodies it should work. There is only one line that needs an identifier : `int z = b.getLength();` – c0der Jul 02 '17 at 16:56

1 Answers1

0

In the second call of BObject constructor, you're passing an argument whereas in it's declaration in the class, it doesn't take any argument. Check that.

Hillux
  • 105
  • 1
  • 2
  • 10
  • It takes a string as an argument – D.B Jul 02 '17 at 19:24
  • It is the method that takes an argument but not the constructor. The constructor is the first one by default am guessing so, since it appears you have named both the method BObject and the constructor BObject hence the first is assumed to be the constructor and it doesn't take any argument. – Hillux Jul 02 '17 at 19:34
  • Methods must have a return type. BObject () is a constructor with no arguments. BObject(String str) is another constructor with one argument. It doesn't matter that they have the same name because they have different parameters. This is called constructor overloading. – D.B Jul 02 '17 at 22:21