public class
Download
Report
Transcript public class
(in Java)
ACCESS MODIFIERS
Four access modifiers used in java:
1. Public
2. Protected
3. no modifier
(declaring without an access modifier)
4. Private
Usage of these access modifiers is restricted to
two levels.
ACCESS MODIFIERS
Usage of these access modifiers is restricted to
two levels.
I) Class level access modifiers (java classes only)
• Only two access modifiers is allowed, public and
no modifier
• If a class is ‘public’, then it CAN be accessed
from ANYWHERE.
• If a class has ‘no modifier’, then it CAN ONLY be
accessed from ‘same package’.
ACCESS MODIFIERS
II) Member level access modifiers (java variables
and java methods)
1. Public
: Visible to the world
2. Protected : Visible to the package and all
subclasses
3. no modifier :Visible to the package. the
default. No modifiers are needed.
4. Private
: Visible to the class only
1. Public Access Modifier
• A class, method, constructor, interface etc
declared public can be accessed from any
other class.
• However if the public class we are trying to
access is in a different package, then the
public class still need to be imported.
• Because of class inheritance, all public
methods and variables of a class are inherited
by its subclasses.
Example of public access modifier
//save by A.java
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
Public Access Modifier
The following function uses public access
control:
public static void main(String[] arguments) {
// ...
}
The main() method of an application has to be
public. Otherwise, it could not be called by a
Java interpreter (such as java) to run the class.
2. Protected Access Modifier
• can be accessed only by the subclasses in
other package or any class within the package
of the protected members' class.
• be applied on the data member, method and
constructor.
Example of protected access modifier
//save by A.java
public class A{
protected void msg(){System.out.println("Hello");}
}
//save by B.java
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
Output:Hello
3. Default Access Modifier - No keyword
• Default access modifier means we do not
explicitly declare an access modifier for a
class, field, method, etc.
• A variable or method declared without any
access control modifier is available to any
other class in the same package.
Example of default access modifier
//save by A.java
package pack;
class A{
void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}
4. Private Access Modifier
• Methods, Variables and Constructors that are
declared private can only be accessed within
the declared class itself.
• A standard design strategy is to make all fields
private and provide public getter methods for
them.
Example of private access modifier
class A{
private int data=40;
private void msg(){System.out.println("Hello java");}
}
public class Simple{
public static void main(String args[]){
A obj=new A();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}
COMPARISION
Access Control and Inheritance
Methods declared
• public in a superclass also must be public in all
subclasses.
• protected in a superclass must either be
protected or public in subclasses; they cannot
be private.
• without access control (no modifier was used)
can be declared more private in subclasses.
• private are not inherited at all, so there is no
rule for them.
If you are overriding any method, overridden method (i.e.
declared in subclass) must not be more restrictive.
class A{
protected void msg(){System.out.println("Hello java");}
}
public class Simple extends A{
void msg(){System.out.println("Hello java");}//C.T.Error
public static void main(String args[]){
Simple obj=new Simple();
obj.msg();
}
}
The default modifier is more restrictive than protected. That is why
there is compile time error.
Tips on Choosing an Access Level
• Use the most restrictive access level that
makes sense for a particular member.
Use private unless you have a good reason not
to.
• Avoid public fields except for constants.
Fields: usually private
Constructors: usually public
Methods: usually public or private