0% found this document useful (0 votes)
13 views22 pages

Chapter 2.2

The document discusses visibility control in Java, detailing four access modifiers: default, private, public, and protected, along with their accessibility rules. It provides examples for each modifier, illustrating how they restrict or allow access to classes, methods, and variables within and outside packages. Additionally, it touches on the implications of these modifiers in method overriding and includes homework questions related to access control parameters.

Uploaded by

yash gavali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views22 pages

Chapter 2.2

The document discusses visibility control in Java, detailing four access modifiers: default, private, public, and protected, along with their accessibility rules. It provides examples for each modifier, illustrating how they restrict or allow access to classes, methods, and variables within and outside packages. Additionally, it touches on the implications of these modifiers in method overriding and includes homework questions related to access control parameters.

Uploaded by

yash gavali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Unit II

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.

The four access levels are −

1. Visible to the package, the default. No modifiers are


needed.
2. Visible to the class only (private).
3. Visible to the world (public).
4. Visible to the package and all subclasses (protected)
2.2. Visibility Control in Java - Default Access Modifier - No Keyword
 If you don't use any modifier, it is treated as default by
default.

 A variable or method declared without any access


control modifier is available to any other class in the same
package.

 The default modifier is accessible only within package.


It cannot be accessed from outside the package.
2.2. Visibility Control in Java - Default Access Modifier - No Keyword

Example:

Variables and methods can be declared without any


modifiers, as in the following examples −

String version = "1.5.1";

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.

 Therefore, fields, methods, blocks declared inside a


public class can be accessed from any class belonging to
the Java Universe.

 However, if the public class we are trying to access is in


a different package, then the public class still needs to be
imported.

 Because of class inheritance, all public methods and


variables of a class are inherited by its subclasses.
2.2. Visibility Control in Java – Public Access Modifier - Public
The following function uses public access control −

public static void main(String[] args)


{
// ...
}

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.2. Visibility Control in Java – Public Access Modifier - Public
//save by A.java //save by B.java

package pack; package mypack;


public class A
{ import pack.*;
public void msg()
{ class B
System.out.print(“Hi..”); {
}
}
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
2.2. Visibility Control in Java – Protected Access Modifier - Protected
 The protected access modifier is accessible within
package and outside the package but through inheritance
only.

The protected access modifier can be applied on the


data member, method and constructor. It can't be applied
on the class.

It provides more accessibility than the default modifer.


2.2. Visibility Control in Java – Protected Access Modifier - Protected
Example:
class A
{
protected void msg()
{
System.out.println("Hello");
} Output:
}
Hello
class B extends A
{
public static void main(String args[])
{
B obj = new B();
obj.msg();
}
}
2.2. Visibility Control in Java

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’

 When no access modifier is specified, the member


defaults to a limited version of public accessibility that is
known as “Friendly or Default Access Modifiers”.

 The difference between the “public” and “friendly or


default” access modifiers is that the public makes fields
visible in all classes including other packages,
while friendly or default makes fields visible only in the
same package, but not in other packages.
2.2. Visibility Control in Java – ‘friendly’ or ‘default’

Example:

int number;
void add()
{
.........
.........
}
2.2. Visibility Control in Java – ‘private protected’

Private Protected makes the fields visible in all


subclasses regardless of what package they are in. These
fields aren’t accessible by other classes in the same
package.
 Example:

private protected int number;


private protected void add()
{
.........
.........
}
2.2. Visibility Control in Java

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

2. Describe access control specifiers with example.


W-17, 4M

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy