Access Modifiers .pptx

77 views 14 slides Sep 07, 2022
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

It helps you to learn Access modifiers On OOp in Java


Slide Content

Access Modifiers 01

Presented by, Name :MD.Rakib Hossen ID :212902020 Section :PC-212(DA) Department: BSc.in CSE in GUB Presented To, DR. MUHAMMAD AMINUR RAHAMAN Associate Professor and Campus Director, Permanent Campus in GUB 02

What Are Access Modifiers? In java , Access modifiers are used to set the accessibility (visibility) of classes, interfaces, variables, methods. 03

Type of Access Modifiers Four type of access modifiers, Default Public Protected Private 04

Default Access Modifiers A default access modifier in Java has no specific keyword. Whenever the access modifier is not specified, then it is assumed to be the default. The entities like classes, methods, and variables can have a default access. A default class is accessible inside the package but it is not accessible from outside the package i.e. all the classes inside the package in which the default class is defined can access this class. 05

Default Access Modifiers For example, void math4(){ result = a+b-c ; System.out.println ("Result by defualt modifier= "+result); } t1.math4(); //try access defualt modifiers in same class. successfully access t2.math4(); //try access defualt modifiers in same pacakge sub class. successfully access t3.math4(); //try access defualt modifiers in same pacakge non-sub class. successfully access //t4.mathy4; //try access defualt modifiers in Different pacakge non-sub class. not successfully access //t5.mathy4; //try access defualt modifiers in Different pacakge sub class. not successfully access 06

Public Access Modifiers A class or a method or a data field specified as ‘public’ is accessible from any class or package in the Java program. The public entity is accessible within the package as well as outside the package. In general, public access modifier is a modifier that does not restrict the entity at all. 07

Public Access Modifiers public void math1(){ result= a+b-c ; System.out.println ("Result print by public modifier= "+result); } t1.math1(); // try access public modifiers in same class. successfully access t2.math1(); // try access public modifiers in same pacakge sub class. successfully access t3.math1(); // try access public modifiers in same pacakge non-sub class. successfully access t4.math1(); // try access public modifiers in Different pacakge non-sub class. successfully access t5.math1(); // try access public modifiers in Different pacakge sub class. successfully access 08

Protected Access Modifiers The protected access modifiers allows access to entities through subclasses of the class in which the entity is declared. It doesn’t matter whether the class is in the same package or different package, but as long as the class that is trying to access a protected entity is a subclass of this class, the entity is accessible. 09

Protected Access Modifiers protected void math3(){ result= a+b-c ; System.out.println ("Result by protected modifier= "+result); } t1.math3(); //try access protected modifiers in same class. successfully access t2.math3(); //try access protected modifiers in same pacakge sub class. successfully access t3.math3(); //try access protected modifiers in same pacakge non-sub class. successfully access // t4.math3(); //try access protected modifiers in Different pacakge non-sub class. not successfully access t5.math3(); //try access protected modifiers in Different pacakge sub class. successfully access 10

Private Access Modifiers The ‘private’ access modifier is the one that has the lowest accessibility level. The methods and fields that are declared as private are not accessible outside the class. They are accessible only within the class which has these private entities as its members. Note that the private entities are not even visible to the subclasses of the class. A private access modifier ensures encapsulation in Java. 11

Private Access Modifiers private void math2(){ result= a+b-c ; System.out.println (" Resultby private modifier= "+result); } t1.math2(); //try access private modifiers in same class. successfully access // t2.math2(); //try access private modifiers in same package sub class. not access successfully access //t3.math2(); //try access private modifiers in same package non-sub class. not access successfully access //t4.math2(); //try access private modifiers in Different package non-sub class. not access successfully access //t5.math2(); //try access private modifiers in Different package sub class. not access successfully access 12

Conclusion 13

Thank You Everyone If you have any question please asked me. 14
Tags