Java provides keyword protected to declare protected data. Here is an example of a program that can show how protected access modifier work:
class A { private int a; //only recognized by class A //only recognized by class A and its derivative class protected void setA (int value) { a = value; } //only recognized by class A and its derivative class protected int getA() { return a; } } class B extends A { private int b; //constructor class B B(int valueA, int valueB) { //a = valueA; //INCORRECT, because a is not recognized here setA(valueA); //using method setA() b = valueB; } public void showData() { //using method getA System.out.println("Value a : " + getA()); System.out.println("Value b : " + b); } } class C { private int c; public void setC(int value) { //set A (10); //INCORRECT, setA() is not recognized here c = value; } public int getC() { return c; } public void showC() { //System.out.println("Value a : " + getA()); //INCORRECT System.out.println("Value c : " + c); } } Class DemoProtected1 { public static void main(String[] args) { B obj = new B(40, 50); obj.showData(); obj.setA(100); System.out.println("Value a : " + obj.getA()): } }
The above program will produce following output:
Value a: 40
Value b: 50
Value a: 100
In the program above, we define method setA() and getA() in class A with protected. Thus, the method can be accessed by derived class (in this case class B). However, when we access the method of class C, there will be error during the compilation. This is because the class C does not have a hierarchical relationship to class A. Here, class C is considered as an external environment which is not allowed to access the protected data from class A.
Thank you so much for the valuable info
ReplyDeleteHere am sharing some information about Best Training Institutes java training in hyderabad |java course in hyderabad | java institutes in hyderabad | software training institutes in hyderabad |selenium training in hyderabad | software training institutes in hyderabad with placements |python training in hyderabad | python course in hyderabad | python institutes in hyderabad
Thanks for sharing such a great blog Keep posting.
ReplyDeletedatabase providers in mumbai
b2b data companies
b2b data providers
relationship intelligence
crm software
sales intelligence
crm software
company database
sales automation tools
Thank You for Sharing valuable information with us.
ReplyDeleteE-Commerce is the fastest-growing platform which is becoming more demandable day by day this is a tremendous form of the vibrant sector that is bringing several opportunities to the new generation to become more skilled and professional. Taking up this great advantage in the E-Commerce sector would need the right path and the right skillset you need to learn the latest technologies and high-demand skills such as building payment gateways and developing an accessible and easy website along with the basics of E-commerce that‘s why Aptech learning official Pro E-Commerce training program in Dwarka, Janakpuri, and Gurgaon which will help to make a successful career in this booming industry. Java Programming Course helps to become an expert in Java web application development. It helps you to bring efficiency in collecting and analyzing the data through Java programming in this course you will learn effective web UI/UX design and Secure web applications by using the advanced technologies of the Java program.
For more details please visit our website : Aptech Janakpuri