1

How to access default class in some/another package using public class in that package.

For example,

my Bank package has 2 classes

  1. public class Bank { ... }

  2. class Account { ... }(default access modifier)

I need to access Account in another package called Atm using Bank.

Any suggestions?

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Refer these links: [Link1]http://www.tutorialspoint.com/java/java_access_modifiers.htm [Link2]http://java-answers.blogspot.in/2012/01/access-specifiers-in-java.html [Link3]http://www.javawithus.com/tutorial/access-specifiers – VVN Mar 02 '16 at 11:23
  • Don't make it default. That means visible in current package only. – Elliott Frisch Mar 02 '16 at 11:23
  • This is one of the tasks in the assignment to make it default. Otherwise, I would make it public) –  Mar 02 '16 at 11:24
  • 1
    Suggest you read the instructions for that task again ... because what you *think* you are being asked to do does not make sense. – Stephen C Mar 02 '16 at 11:26
  • Possible duplicate of [How import java classes inside the default package from the default package](http://stackoverflow.com/questions/35676346/how-import-java-classes-inside-the-default-package-from-the-default-package) – Ricard Nàcher Roig Mar 02 '16 at 11:29
  • 1
    I need to access default class using public class in the same package. –  Mar 02 '16 at 11:29
  • As written before: this assignment does not sound valid. If Atm is NOT in the same package as Bank and Account; the compiler will not allow you to use Account within Atm. – GhostCat Mar 02 '16 at 11:37
  • Accessing a default ("package protected") class from outside the package is not possible. This is nicely illustrated in [this table](http://stackoverflow.com/a/33627846/276052). If you want to keep some control of the `Account` instances within the `Bank` package, you can make the `Account` class public, but its constructor package protected (so that it can only be created from within the package) and then create a public account factory method in the `Bank` package. – aioobe Mar 08 '16 at 20:57

3 Answers3

1

You can't access this class from another package directly, but you can use proxy pattern and call Account methods by calling Bank methods

Javasick
  • 2,753
  • 1
  • 23
  • 35
  • You understood me. This is what I want to do –  Mar 02 '16 at 11:32
  • 1
    create a public class in the same package with your default class then use Proxy pattern or Delegate pattern to access the default class. Eclipse supports auto generate delegate pattern. From other packages, you can access the default class via the public class – HungNM2 Sep 05 '21 at 09:45
0

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

Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
  • So you mean that I cannot access it even I will create instance of it in public class? –  Mar 02 '16 at 11:31
  • Absolutely. Calling class knows nothing about type Account – Javasick Mar 02 '16 at 11:32
  • default class is not visible to any where out side the package, so how will you create instance of it into public class (another package ) ? – Vishal Gajera Mar 02 '16 at 11:33
  • 1. Create default class in public class in the same package 2. Access it in another package calling public class –  Mar 02 '16 at 11:38
  • @Atalyk are you talking about inner class(class within class) ? – Vishal Gajera Mar 02 '16 at 11:39
  • @vishalgajera No, it is not –  Mar 02 '16 at 12:00
  • @Atalyk then do you mean, into same package both classes resides ? – Vishal Gajera Mar 02 '16 at 12:01
  • @vishalgajera Yes, both Account and Bank classes are in the same package. I need to access Account methods in Atm class via Bank class. Like this one int balance = bank.getAccounts().get(customerID).getBalance(); but The Account is not visible –  Mar 02 '16 at 12:13
  • @Atalyk according your question asked, inside it you have clearly mentioned both class belongs into different packages. i.e. Bank package and Atm package. so please be clear first. – Vishal Gajera Mar 02 '16 at 12:15
  • @vishalgajera Yes, there are 2 packages Bank (Bank.class - public, Account.class - private) and Atm (Atm.class - public). Then I need to access Account.class methods in Atm.class via Bank.class. Like this one int balance = bank.getAccounts().get(customerID).getBalance(); but The Account is not visible –  Mar 02 '16 at 12:18
  • @vishalgajera This is like real world ATM which uses bank to access Accounts. –  Mar 02 '16 at 12:20
  • please , point your corrent issue rightly, you issue is private Account class's member not accessible to Bank class, see private things can not be visible to out of class. so remove private access modifier so that Account class become's default which will work then – Vishal Gajera Mar 02 '16 at 12:20
  • @vishalgajera I'm sorry for not being clear. Actually I need access Account.class methods in Atm.class which is another package. In order to do so I need to use public Bank.class which stores all accounts –  Mar 02 '16 at 12:22
  • okay, now do one thing, Bank class can access Account.class(make it default remove private modifier ) and then create one method into Bank class which will access Account, finally Atm class access Bank which will access Account. – Vishal Gajera Mar 02 '16 at 12:24
  • @vishalgajera Can I call methods without creating Account object? –  Mar 02 '16 at 12:30
  • Yes, you can call Bank class's method. – Vishal Gajera Mar 02 '16 at 12:31
0

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.

Luca Putzu
  • 1,438
  • 18
  • 24
  • Like this. Basically I want to call Account methods inside Atm class Account account = (Account)bank.getAccounts().get(customerID); but Account is not visible. –  Mar 02 '16 at 11:46
  • @Atalyk: are not Bank and Account in the same package? – Luca Putzu Mar 02 '16 at 12:34