Chapter 2.2
Chapter 2.2
Derived Syntactical
Constructs in Java
Mr. R. M. Patil
SY CO, JPR
2023-2024
2.2. Visibility Control in Java
Java provides a number of access modifiers to set
access levels for classes, variables, methods, and
constructors.
Example:
boolean processOrder()
{
return true;
}
2.2. Visibility Control in Java - Default Access Modifier - No Keyword
2.2. Visibility Control in Java – Private Access Modifier - Private
Methods, variables, and constructors that are declared
private can only be accessed within the declared class
itself.
Private access modifier is the most restrictive access
level. Class and interfaces cannot be private.
Variables that are declared private can be accessed
outside the class, if public getter methods are present in
the class.
Using the private modifier is the main way that an
object encapsulates itself and hides data from the outside
world.
2.2. Visibility Control in Java – Private Access Modifier - Private
Example:
we have declared a private
class Data variable named ‘name’.
{
private String name; We are accessing private
} member from outside the
class, so there is a
public class test compile-time error.
{
public static void main(String [] args)
{
Data d = new Data();
d.name = “sit"; //Compile Time Error
}
}
2.2. Visibility Control in Java – Private - Example
class A
{ In this example, we
private int data=40; have created two
private void msg()
{
classes A and Simple.
System.out.println("Hello java");
} A class contains
} private data member
and private method.
public class Simple
{
public static void main(String args[]) We are accessing
{ these private
A obj=new A(); members from
System.out.println(obj.data); //Compile Time Error outside the class, so
obj.msg(); //Compile Time Error
}
there is a compile-
} time error.
2.2. Visibility Control in Java – Private - Example
class A
{ Role of Private
private A() //private constructor Constructor :
{}
void msg()
{ If you make any
System.out.println("Hello java");
} class constructor
} private, you
public class Simple cannot create
{ the instance of
public static void main(String args[])
{
that class from
A obj=new A(); //Compile Time Error outside the
}
}
class.
2.2. Visibility Control in Java – Public Access Modifier - Public
A class, method, constructor, interface, etc. declared
public can be accessed from any other class.
outside
within within package by outside
Access Modifier
class package subclass package
only
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
2.2. Visibility Control in Java – ‘friendly’ or ‘default’
Example:
int number;
void add()
{
.........
.........
}
2.2. Visibility Control in Java – ‘private protected’
outside
within within package by outside
Access Modifier
class package subclass package
only
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
Java Access Modifiers with Method Overriding
class A
{
protected void show()
{ Compile Time Error:
System.out.println("Hi...");
} error: show() in demo cannot override show()
} in A
class demo extends A void show()
{ ^
void show()
{
attempting to assign weaker access privileges;
System.out.println("bye...");
was protected
} 1 error
public static void main(String [] args)
{
demo d = new demo();
d.show();
}
}
Java Access Modifiers with Method Overriding
class A
{
void show()
{ Output:
System.out.println("Hi...");
} bye……
}
class demo extends A
{
void show()
{
System.out.println("bye...");
}
public static void main(String [] args)
{
demo d = new demo();
d.show();
}
}
Homework
1. What are the access control parameters? Explain the
concept with suitable example.
W-16, 6M