Learn To Use Public, Protected, PACKAGE-PRIVATE And Private Access Modifiers In Java

Опубликовано: 17 Октябрь 2024
на канале: Begin Secure
68
1

Find out the difference between public, protected, private and package-private access modifiers in Java. Find out the application security concerns about access modifiers and how to keep your code secure. Learn some tips about using IntelliJ IDEA too.

#IT #Development #SoftwareDevelopment #JavaProgramming #JavaTutorial #AppplicationSecurity #AppSec #appsecurity #informationsecurity #infosec

Chapters

00:00 Introduction
00:50 Overview
01:05 Where are access modifiers used?
01:46 Public access modifier described
02:16 Package-private access modifier described
02:56 Private access modifier described
03:18 Protected access modifier described
03:51 Access modifier chart of use
05:04 Coding example showing how access modifiers impact visibility
16:57 Application security discussion of access modifiers

The question today is what is the difference between the access control modifiers in Java?

Java provides access modifiers to control access to fields, methods and classes

The keywords, which are also reserved words are

public

protected

private

and the default modifier which list here as ‘no modifier’ or package-private
no modifier is not a modifier per se, it is simply the lack of an explicit public, protected or private modifier, resulting in default modifier

In the coding section coming up, we’ll go deep on each one and demonstrate in code all the different levels of access each modifier provides

But before that, let’s look at each of the modifiers and later we will compare them in a table, side by side

First let’s look at public

A class may be declared public meaning it is visible to any class anywhere within the application that uses the class

At the member level -- and here member means a method or a field within a class – public means any code from any location has access

Public access should be used with care since it exposes access to the world. We’ll discuss this topic in more detail when we cover the security aspects of access control later in this video

Next is package-private

Package-private is not a modifier we specify explicitly, rather it is an implicit modifier when no modifier is explicitly stated,

If no modifier such as public, private or protected is specified, package-private rules come into play

Classes can be public or package-private only