Java has four
access specifies:
1.
Public
2.
Private
3.
Protected
4.
Default
Class
A class can
be defined either as public or default. Note: default is not a keyword as
public. When no access specifier is mentioned default access specifier is
applied.
E.g.
package my;
public class BankAccount {
}
A .java file can have only one public class. And the
name of the file should be the same as the public class name. A java file should have atleast one
public class. It can have more than one default classes.
class DefaultBankAccount {
}
SO the class DefaultBankAccount can be placed in the
BankAccount.java file.
|
Difference in using public class and default class:
Consider this
test class which is in the same package my
package my;
public class Test {
public static void main(String[] args)
{
BankAccount
bankAccount = new BankAccount();
DefaultBankAccount
defaultBankAccount = new DefaultBankAccount();
}
}
The main
method here is able to create the object of BankAccount and DefaultBankAccount since
Now if we
move Test class in some other package, we will get en error on line 2 where we
are accessing DefaultBankAccount class.
That means a
default class can only be accessed from method in a class which is in same
package. While public class can be accessed from any class in any package in
the same project.
Similarly the
Test class can have a instance variable of BankAccount or can extend
BankAccount but not DefaultBankAccount if Test class is in another package.
public class Test extends BankAccount{
BankAccount account;
}
Instance variables/methods
Instance variables can have all 4 access specifiers. Public,
private, protected and default.
Public
Let’s say BankAccount class has a instance variable as
accountNumber;
public class BankAccount {
public int accountNumber;
}
The test class which is in another package will be able to
access it.
public class Test {
public static void main(String[] args)
{
BankAccount
bankAccount = new BankAccount();
System.out.println(bankAccount.accountNumber);
}
}
And will not be able to access any DefaultBankAccount
instance variables since the class DefaultBankAccount is itself unacessible.
Private
If the same instance variable is made private, Test class
will give error when trying to access accountNumber. A private instance
variable can be accessed only from inside the class. So a method inside
BankAccount can access it.
public class BankAccount {
private int accountNumber;
public int getAccountNumber(){
return
accountNumber;
}
}
And the test class can call the getAccountNumber since this
is a public method.
A class cannot be marked private.
Default
If the instance variable is marked as default than the Test
class if it is same package can access it. But if the Test class is in another
package it will not be able to access it.
Protected
This is a special case. If an instance variable is marked as
protected it can be accessed from any class in the same package. So in the same
package it act as default.
But if the package of the calling class is different it can
be accessed only throw inheritance.
Let’s see:
public class Test extends BankAccount{
public static void main(String[] args)
{
Test
test = new Test();
System.out.println(test.accountNumber);
}
}
A Test class is extending BankAccount and the accountNumber
instance variable is accessed on Test class object (through inheritance).
Note: A instance variable or method is accessible only if
the class is accessible. So even if a instance variable is marked as public but
belongs to a default class it will not be accessed from a class in another
package, Because the Test class will not be able to create instance of the
Default class.
The local variables inside a method do not have access
specifier. Their life is only for the execution of the method.
public void addSomething() {
int i = 8;
int j = 8;
System.out.println(i + j);
}
Here i and j are local variables and not instance variables.
They do not have any access specifier.