Access modifiers can be used to specify the scope or visibility of the class or the members of the class.
There are four access modifiers:
a. public
b. protected
c. default
d. public
Private scope:
-------------
1. Private scope is also called class scope, i.e private members must be accessed from the same class where it is declared.
2. Private members cannot be accessed from outside the class, not even from sub class i.e private members will not be inherited to sub classes.
3. Private modifier can be used only for members of the class not for class itself.
Default scope:
--------------
1. When you are not using any using any modifier with the members of the class then the scope of the members will be default scope.
2. Default scope is also called package scope i.e default members can be accessed from the same class, sub class and non sub class which are in the same package.
3. Default members cannot be accessed from outside the package i.e default members will not be inherited to sub class available in outside the package.
4. Default scope is available for both class and its members.
Protected scope:
----------------
1. Protected members can be accessed from the same class, sub class and non sub class which are available in the same package.
2. Protected members can be accessed from the sub classes which are available in different package.
3. Protected members cannot be accessed from the non sub classes which are available in different package.
4. Protected modifier can be used only for members of the class not for class itself.
Public scope:
-------------
1. Public members can be accessed from anywhere.
2. Public modifier can be used for both class and their members.
Example 1: The following programs demonstrate how member variables of class - A can be accessed from classes in the same package and different package.
Accessing inherited member variables of class - A from a different class in the same package
Accessing object of A from a class in the same package
Subclass of class - A from a different package
Accessing object of class - A from a different package.
Test class to execute all the above classes
There are four access modifiers:
a. public
b. protected
c. default
d. public
Private scope:
-------------
1. Private scope is also called class scope, i.e private members must be accessed from the same class where it is declared.
2. Private members cannot be accessed from outside the class, not even from sub class i.e private members will not be inherited to sub classes.
3. Private modifier can be used only for members of the class not for class itself.
Default scope:
--------------
1. When you are not using any using any modifier with the members of the class then the scope of the members will be default scope.
2. Default scope is also called package scope i.e default members can be accessed from the same class, sub class and non sub class which are in the same package.
3. Default members cannot be accessed from outside the package i.e default members will not be inherited to sub class available in outside the package.
4. Default scope is available for both class and its members.
Protected scope:
----------------
1. Protected members can be accessed from the same class, sub class and non sub class which are available in the same package.
2. Protected members can be accessed from the sub classes which are available in different package.
3. Protected members cannot be accessed from the non sub classes which are available in different package.
4. Protected modifier can be used only for members of the class not for class itself.
Public scope:
-------------
1. Public members can be accessed from anywhere.
2. Public modifier can be used for both class and their members.
Example 1: The following programs demonstrate how member variables of class - A can be accessed from classes in the same package and different package.
package com.pack.p1; public class A{ private int a=10; int b =20; protected int c=30; public int d=40; public void showA(){ System.out.println("A->showA()"); System.out.println("#Direct#"); System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); System.out.println("#A-Object#"); A a1 = new A(); System.out.println(a1.a); System.out.println(a1.b); System.out.println(a1.c); System.out.println(a1.d); } }
Accessing inherited member variables of class - A from a different class in the same package
package com.pack.p1; public class B extends A{ public void showB(){ System.out.println("B->showB()"); System.out.println("#Direct#"); //System.out.println(a); private member of super class is not inherited System.out.println(b); //default is inherited System.out.println(c); //protected is inherited (package scope) System.out.println(d); //public is inherited System.out.println("#A-Object#"); A a1 = new A(); //System.out.println(a1.a); private member of a class cannot be accessed outside the class. System.out.println(a1.b); //default is inherited and can be accessed System.out.println(a1.c); //protected is inherited and can be accessed System.out.println(a1.d); //public is inherited and can be accessed System.out.println("#B-Object#"); B b1 = new B(); //System.out.println(b1.a); private member of super class is not inherited System.out.println(b1.b); //default is inherited and can be accessed System.out.println(b1.c); //protected is inherited and can be accessed System.out.println(b1.d); //public is inherited and can be accessed } }
Accessing object of A from a class in the same package
package com.pack.p1; public class C{ public void showC(){ System.out.println("C->showC()"); System.out.println("#A-Object#"); A a1 = new A(); //System.out.println(a1.a); private member cannot be accessed outside the class. System.out.println(a1.b); System.out.println(a1.c); System.out.println(a1.d); } }
Subclass of class - A from a different package
package com.pack.p2; import com.pack.p1.A; public class D extends A{ public void showD(){ System.out.println("D->showD()"); System.out.println("#Direct#"); //System.out.println(a); private member will not be inherited //System.out.println(b); default member will not be inherited. System.out.println(c); System.out.println(d); System.out.println("#D-Object#"); D d1 = new D(); //System.out.println(d1.a); private member cannot be accessed outside the class. //System.out.println(d1.b); default member cannot be accessed outside the package. System.out.println(d1.c); System.out.println(d1.d); System.out.println("#super keyword#"); //System.out.println(super.a); private member will not be inherited //System.out.println(super.b); default member cannot be accessed outside the package. System.out.println(super.c); System.out.println(super.d); A a1 = new A(); System.out.println("#A-Object#"); //System.out.println(a1.a); private member cannot be accessed outside the class. //System.out.println(a1.b); default member cannot be accessed outside the package. //System.out.println(a1.c); protected member cannot be accessed outside the package using the super class object. System.out.println(a1.d); } }
Accessing object of class - A from a different package.
package com.pack.p2; import com.pack.p1.A; public class E{ public void showE(){ System.out.println("E->showE()"); A a1 = new A(); //System.out.println(a1.a); private member cannot be accessed outside the class. //System.out.println(a1.b); default member cannot be accessed outside the package. //System.out.println(a1.c); protected member cannot be accessed outside the package if it's not inherited. System.out.println(a1.d); //public can be accessed from anywhere. } }
Test class to execute all the above classes
package com.pack.p3; import com.pack.p1.*; import com.pack.p2.*; public class Lab2{ public static void main(String args[]){ A a1 = new A(); a1.showA(); B b1 = new B(); b1.showB(); C c1 = new C(); c1.showC(); D d1 = new D(); d1.showD(); E e1 = new E(); e1.showE(); } } /* C:\JavaPractice\JLC Notes and Examples\Access Modifiers\src>java -cp ..\classes com.pack.p3.Lab2 A->showA() #Direct# 10 20 30 40 #A-Object# 10 20 30 40 B->showB() #Direct# 20 30 40 #A-Object# 20 30 40 #B-Object# 20 30 40 C->showC() #A-Object# 20 30 40 D->showD() #Direct# 30 40 #D-Object# 30 40 #super keyword# 30 40 #A-Object# 40 E->showE() 40 */
No comments:
Post a Comment