简体   繁体   中英

how do i access a public method of a default class outside the package

I have a class with no modifier(default), which has a public method called mymeth. I know I could access the method when I am within the package. However I would like to access the method when I am outside the package. does anyone has an Idea on how it could be done. theoretically I think it should be possible since public method means access by the world. here is the example of my class and method:

class myclass{

public void mymeth(int i,int b){

.....
  }

}

set myclass class to be public .

**FYI, Classes in Java start from upper Case letter

myclass设置为public然后将其放在使用myclass所需的类的构建路径中。

public interface SomeInterface{
   public void mymeth();
}

class MyClass implements SomeInterface{

   public void mymeth(){

   }
}

//is in the same package as MyClass
public MyClassFactory{
   public SomeInterface create(/*parameters*/){
       //create instance from parameters
       //For your case
       MyClass instanceOfAnyClassThatImplementsSomeInterface = new MyClass(/*pass the parameters*/); 
       return instanceOfAnyClassThatImplementsSomeInterface;
   }
}

One of the ways is already defined in answers but If you want to restrict the public access of the class then you can create an interface and access the method through it.

In your code, myclass has the default (package-level) access modifier. It should be declared using the public access modifier so that it is accessible outside its package. For details, read more about Controlling Access in Java .

As a side note, the Java standards require you to capitalize each word in the class name, so you should use MyClass . I recommend you the Java Conventions document .

Consider making another public class MyChild with the same package name as MyClass and expose the method from MyChild class

public class MyChild extends MyClass {

  public void myTestMethod(){
     super.myTestMethod
   }

}

Now in your class where you want to use the method, simply use the instance of MyChild class

MyChild m = new MyChild();

m.myTestMethod();

Cheers :)

Directly you cannot. 'public' makes everything visible. But if you can't see the class, it's difficult to call anything. However,

You can extend the default class with a public class, eventually myMeth is exposed.

PubClass.java

package p1;

class DefClass{
    public void myMeth(){
        System.out.println("from myMeth!");
    }
}

public class PubClass extends DefClass{
    public PubClass(){
        super();
    }
}

MainClass.java

package p2;

class MainClass {

    public static void main(String[] args) {
        p1.PubClass pub = new p1.PubClass();

        pub.myMeth();
    }

}

output:

from myMeth!

A real practical use for this would be, overriding a public known method in that hidden class. You can implement a public method in a hidden class, so the world can call your public method (public implementation rather) without the class being exposed. For example the public method of the Object class is overridden here by DefClass:

PubClass.java

package p1;

class DefClass{
    public String toString(){
        return "DefClass here. Trying to explain a concept.";
    }
}

public class PubClass extends DefClass{
    public PubClass(){
        super();
    }
}

MainClass.java

package p2;

class MainClass {

    public static void main(String[] args) {
        p1.PubClass pub = new p1.PubClass();
        System.out.println(pub.toString());
    }    
}

output:

DefClass here. Trying to explain a concept.

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