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.
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.
modifiers in java specifies accessibility (scope) of a data member, method,
constructor or class.
There are 4
types of java access modifiers:
types of java access modifiers:
- private
- default
- protected
- public
There are
many non-access modifiers such as static, abstract, synchronized, native,
volatile, transient etc. Here, we will learn access modifiers.
many non-access modifiers such as static, abstract, synchronized, native,
volatile, transient etc. Here, we will learn access modifiers.
-------------------------
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();
}
}
No comments:
Post a Comment