eskoba
eskoba

Reputation: 566

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){

.....
  }

}

Upvotes: 0

Views: 3027

Answers (6)

mathmax
mathmax

Reputation: 37

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.

Upvotes: 1

Kumar Mani
Kumar Mani

Reputation: 161

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 :)

Upvotes: 0

Narendra Pathai
Narendra Pathai

Reputation: 41935

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.

Upvotes: 0

aUserHimself
aUserHimself

Reputation: 1597

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.

Upvotes: 0

davide
davide

Reputation: 1948

Set myclass as public then put it in the build path of the class you need to use myclass.

Upvotes: 0

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

set myclass class to be public.

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

Upvotes: 1

Related Questions