Java – protected keyword – Assignment

Learn the significance of ‘protected’ keyword in Java by doing the below Assignment.

Step #I
1) Create a package ‘India’
2) In this package, create a public class ‘Parent’
3) Have below ‘default’ variables and methods
String name=”abcd”;
int age = 60;
void attendFunctions(){}
void goToShop(){}

Step #II
1) In the same package ‘India’, create a public class ‘Sibling’. Make this class as Child class of ‘Parent’
2) Have main method in this class
3) Create instance for Sibling class here
4) Access all variables and methods from Parent Class here.

Step #III
1) Create a package called ‘Canada’
2) Create a public class with your name ‘Prathap’. Make this class as Child class of ‘Parent’
3) Have main method in this class
4) Create instance for ‘Prathap’ class here
5) Access all variables and methods from Parent Class here.

Observation:
Default variables, methods can’t be accessed out side the package

Step #IV:
1) Go to Parent Class
2) Add below variables and methods as protected
protected int amount = 10000;
protected updateAccount()
{

} 

3) Now, go to ‘Prathap’ class
4) Access these protected variable and method of parent class.

Observation:
Can access protected variables and methods from Child class
outside the package

Step #V:
1) Create a package called ‘Canada’
2) Create a public class with your name ‘Prathap_Friend’. ‘
3) Have main method in this class
4) Create instance for ‘Prathap_Friend’ class here
5) Access protected variables and methods from Parent Class here.

Observation:
This class can’t access protected variables and methods.