Reputation:
How to access default class in some/another package using public class in that package.
For example,
my Bank package
has 2 classes
public class Bank { ... }
class Account { ... }
(default access modifier)
I need to access Account
in another package
called Atm
using Bank
.
Any suggestions?
Upvotes: 1
Views: 3098
Reputation: 1456
Default access modifier for classes in java are, by definition, accessible only from within its package (see here).
If you have access to source code you should consider to change the access level to public. Otherwise you can try to access that class via a public class in the same package.
package test.bankaccount;
public class Bank {
public Account getAccount(int id) {
//here goes the code to retrieve the desired account
}
}
package test.bankaccount;
class Account {
// class implementation
}
Anyway you should keep in mind that access restrictions always describes how application is meant to work. You should ask yourself why a specific class is not public.
If the classes are your own code, then ask yourself if the access restriction you put on represents correctly the way you intend the application to work.
Upvotes: 0
Reputation: 2993
You can't access this class from another package directly, but you can use proxy pattern and call Account methods by calling Bank methods
Upvotes: 1
Reputation: 4207
As per java's Rule for Accessing class/method/instances
,
the default things(class/method/instances) must not be visible into another package.
So, here in your case it's not possible to access it through another package because default class not visible to there.
default things visible within same package only where it's define/declare
Upvotes: 0