简体   繁体   中英

My main method can't see all public methods in a different package in java

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM