Thursday, March 10, 2016

Understanding all Java Access Modifiers

Understanding all java access modifiers(Access Modifiers)

Let's understand the access modifiers by a simple table.




Applying access modifier with method overriding

If you are overriding any method, overridden method (i.e. declared in subclass)
must not be more restrictive.

    class A{ 
    protected void msg(){System.out.println("Hello java");} 
    } 
     
    public class Simple extends A{ 
    void msg(){System.out.println("Hello java");}//C.T.Error 
     public static void main(String args[]){ 
       Simple obj=new Simple(); 
       obj.msg(); 
       } 
    } 

The default modifier is more restrictive than protected. That is why there is compile time error.

No comments:

Post a Comment