My dear friends today my aim is to tell you about one of the most important OPP lesson.
That is ACCESS MODIFIERS....
What are access modifires..
Access modifires are used to control accessing a class by another class
Access modifires may be public,private,protected,default and so on.
Now I will describe this modifires one by one.
First oen is PUBLIC access modifire.
Public access modifier can apply classes, instance variables, methods, and constructors.
It can access anywhere in the program (more generous).
Eg: Run the below program and you will understand what is PUBLIC access modifire..
Save below codes as Fruit.java
public class Fruit{
public void getName(){
System.out.println("This is a nice fruit");
}
}
See we have define a class called Fruit above.The access modifire is PUBLIC.Therefore it is open to the outside.
It can be access easity by other class.The method inside in it is also public.So it can be access easily by instantiating.
Now save the below codes as Banana.java in the same directory..
public class Banana extends Fruit{
public static void main(String args[]){
Fruit f=new Fruit();
f.getName();//Here we are accessing Fruit class getName method.
}
}
Now you will see below result....
That is ACCESS MODIFIERS....
What are access modifires..
Access modifires are used to control accessing a class by another class
Access modifires may be public,private,protected,default and so on.
Now I will describe this modifires one by one.
First oen is PUBLIC access modifire.
Public access modifier can apply classes, instance variables, methods, and constructors.
It can access anywhere in the program (more generous).
Eg: Run the below program and you will understand what is PUBLIC access modifire..
Save below codes as Fruit.java
public class Fruit{
public void getName(){
System.out.println("This is a nice fruit");
}
}
See we have define a class called Fruit above.The access modifire is PUBLIC.Therefore it is open to the outside.
It can be access easity by other class.The method inside in it is also public.So it can be access easily by instantiating.
Now save the below codes as Banana.java in the same directory..
public class Banana extends Fruit{
public static void main(String args[]){
Fruit f=new Fruit();
f.getName();//Here we are accessing Fruit class getName method.
}
}
Now you will see below result....