public class forloop { public static void main(String[] args) { for(int i=1;i<=10;i++){ System.out.println(i); } } }
Output
When compile the code output will be as follows.
1 2 3 4 5 6 7 8 9 10
class A{ private int data=78; private void msg(){System.out.println("Hello SPlessons");} } public class Simple{ public static void main(String args[]){ A obj=new A(); System.out.println(obj.data);//Compile Time Error obj.msg();//Compile Time Error } }
Here developer created two classes A and Simple. In class A, one private data member and one private method are there at the same time developer is calling those from outside the class, then the developer will get compile time error as shown in the above example.
package pack; public class A{ protected void splesson(){System.out.println("Hello Splessons");} }
Here created a class with the name A and given specifier as protected to the method, Hello Splessons is the message will printed from the second class.
B.java
package mypack; import pack.*; class B extends A{ public static void main(String args[]){ B obj = new B(); obj.splesson(); } }
Output
When compile the code output will be as follows.
Hello Splessons
Protected modifier is the somewhat precarious and user can state is a superset of the default modifier. Protected members are same as the default members to the extent the access in a similar package. The distinction is that, the protected members are likewise open to the subclasses of the class in which the class is announced which are outside the package in which the parent class is available. By using inheritance concept protected members are accessible for outside package also.