Today I am going to tell you about another access modifier.It is private.
It is easy...
We have to understand some conventions about the PRIVATE access modifire..
1. It can not be used in classes,Interfaces.
2. It can be used in methods,instance variables and construtors.
3. But in Interfaces ve can not use this modifire in methods.
Now save the below codes as Brid.java
public class Bird{
private void getName(){
System.out.println("My parent class is Bird");
}
}
See the above class.It is a public class and inside this class there is a private method called getName.
Now save below codes as Hen.java
public class Hen extends Bird{
public static void main(String args[]){
Bird b=new Bird();
b.getName();//here we are going to access private method of Bird class.
}
}
In above class we are going to access the getName private method.See the below and you will understand that we can not do it.
It is a compilation error!!!!.
Conclusion : Private method of one class can not be accessed within another class.
It is easy...
We have to understand some conventions about the PRIVATE access modifire..
1. It can not be used in classes,Interfaces.
2. It can be used in methods,instance variables and construtors.
3. But in Interfaces ve can not use this modifire in methods.
Now save the below codes as Brid.java
public class Bird{
private void getName(){
System.out.println("My parent class is Bird");
}
}
See the above class.It is a public class and inside this class there is a private method called getName.
Now save below codes as Hen.java
public class Hen extends Bird{
public static void main(String args[]){
Bird b=new Bird();
b.getName();//here we are going to access private method of Bird class.
}
}
In above class we are going to access the getName private method.See the below and you will understand that we can not do it.
It is a compilation error!!!!.
Conclusion : Private method of one class can not be accessed within another class.
That is the nature of private access modifire.
But if you change the access modifire pf method to public ypu can access that method.Try to do it.
That is all for today..be happy!!! :D
But if you change the access modifire pf method to public ypu can access that method.Try to do it.
That is all for today..be happy!!! :D