How to Access package private Class from a Class in some other package?

后端 未结 10 1290
情歌与酒
情歌与酒 2020-12-13 21:57

I have following classses

Hello.java

package speak.hello;

import java.util.Map;

import speak.hi.CustomMap;
import speak.hi.Hi;

public cla         


        
10条回答
  •  星月不相逢
    2020-12-13 22:37

    1st class is in package a.b.c.class1 but class1 is private as well as abstract 2nd class is in package a.b.c.class2 extends class1 but class2 is public

    3rd class is in package x.y.z.class3

    So as to access class1 in class 3 you can write something like:-

    Class baseClass = (new class2()).getClass(); and use the instance of its superclass then use:- baseClass.getSuperClass(); and use it wherever you want.

    But then again the baseclass was made abstract and private for a reason hence not advisable to do so but then again this solution could be used as a workaround.

提交回复
热议问题