Showing posts with label Learn Java In Hindi. Show all posts
Showing posts with label Learn Java In Hindi. Show all posts

Sunday, 25 March 2018

What Are The Access Specifiers/Modifiers In Java || Learn Java In Hindi



Only 3.9% of viewers are subscribing to my channel 😓.
I request you to please give click on Subscribe button.
It really helps me grow 😢.




Access Modifiers in java
There are
two types of modifiers in java: access modifiers and non-access
modifiers.
The access
modifiers in java specifies accessibility (scope) of a data member, method,
constructor or class.
There are 4
types of java access modifiers:
  1. private
  2. default
  3. protected
  4. public
There are
many non-access modifiers such as static, abstract, synchronized, native,
volatile, transient etc. Here, we will learn access modifiers.

Source Codes

-------------------------

A.java

public class A
{
public void m1()
{
System.out.println("public : m1 method");
}
protected void m2()
{
System.out.println("protected : m2 method");
}
void m3() // default - no access
{
System.out.println("no access : m3 method");
}
private void m4()
{
System.out.println("private : m4 method");
}
void call1()
{
m1();
m2();
m3();
m4();
}
}

class test
{
public static void main(String args[])
{
A a1 = new A();
a1.call1();
}
}


B.java


class B extends A
{
void call2()
{
// m4(); // private method can't be accessed here outside the class
// rest methods can be accessed easily here
m1();
m2();
m3();
}
}

class test1
{
public static void main(String srags[])
{
B b1 = new B();
b1.call2();
}
}

C.java


class C
{
void call3()
{
new A().m1();
new A().m2();
new A().m3();
}
}
class test2
{
public static void main(String args[])
{
C c1 = new C();
c1.call3();
}
}