Access Modifier : Private

in Hindi - Access Modifier : Private



not yet

Source Code :

RUN<div class="col-sm-12">
<div class="col-sm-6">
<h4>With private</h4>
<pre>
class Myclass1{ 

private int x=4;
private int y=6;
void run1(){
System.out.println("Value of x is "+x);
}
}
class Myclass2 extends Myclass1{ 

public void run2(){
System.out.println("Value of y is "+y);//error: y has private access in Myclass1
}
}
class Demo5{ 
public static void main(String args[]){ 
Myclass1 m1=new Myclass1();
Myclass2 m2=new Myclass2(); 
m1.run1();
m2.run2();
} 
}</pre>
</div>
<div class="col-sm-6" style="margin-top:-18px">
<h4>Without private</h4>
<pre>
class Myclass1{ 

private int x=4;
int y=6;
void run1(){
System.out.println("Value of x is "+x);
}
}
class Myclass2 extends Myclass1{ 

public void run2(){
System.out.println("Value of y is "+y);//error: y has private access in Myclass1
}
}
class Demo5{ 
public static void main(String args[]){ 
Myclass1 m1=new Myclass1();
Myclass2 m2=new Myclass2(); 
m1.run1();
m2.run2();
} 
}

Output :
Value of x is 4
Value of y is 6
</pre>
</div>
</div>