150 likes | 268 Views
Modifier in Java. Modifier in java. Modifier in Java. * Suatu akses ke classes, constructors, methods, dan fields. Dapat mengkontrol informasi yang dapat diakses oleh class lain. Modifier in java: in same package. Modifier in java: in same package. BaseClass. Extends.
E N D
Modifier in Java *Suatuakseske classes, constructors, methods, dan fields. Dapatmengkontrolinformasi yang dapatdiaksesoleh class lain.
Modifier in java: in same package BaseClass Extends SubclassInSamePackage
SamePackage:1 package pckage1; class BaseClass { public int x = 10; private int y = 10; protected int z = 10; int a = 10; //Implicit Default Access Modifier public class SubclassInSamePackage extends BaseClass { public static void main(String args[]) { BaseClassrr = new BaseClass(); rr.z = 0; SubclassInSamePackagesubClassObj = new SubclassInSamePackage();
SamePackage:2 package pckage1; class BaseClass { public int x = 10; private int y = 10; protected int z = 10; int a = 10; System.out.println("Value of x is : " + subClassObj.x); subClassObj.setX(20); System.out.println("Value of x is : " + subClassObj.x); //System.out.println("Value of y is : "+subClassObj.y); //subClassObj.setY(20); *Private System.out.println("Value of z is : " + subClassObj.z); subClassObj.setZ(30); System.out.println("Value of z is : " + subClassObj.z); System.out.println("Value of x is : " + subClassObj.a); subClassObj.setA(20); System.out.println("Value of x is : " + subClassObj.a); Value of x is :10 Value of x is : 20
SubclassDifferent Package:1 BaseClass Extends SubclassInSamePackage Extends SubClassInDifferentPackage
SubClassDifferent Package:2 public class SubclassInSamePackage extends BaseClass { public static void main(String args[]) { BaseClassrr = new BaseClass(); rr.z = 0; SubclassInSamePackagesubClassObj = new SubclassInSamePackage(); import pckage1.*; public class SubClassInDifferentPackage extends SubclassInSamePackage { public static void main(String args[]) { SubClassInDifferentPackagesubClassDiffObj = new SubClassInDifferentPackage(); SubclassInSamePackagesubClassObj = new SubclassInSamePackage();
SubClassDifferent Package:3 package pckage1; class BaseClass { public int x = 10; private int y = 10; protected int z = 10; int a = 10; //Implicit Default Access Modifier System.out.println("Value of x is : " + subClassObj.x); subClassObj.setX(30); System.out.println("Value of x is : " + subClassObj.x); /* System.out.println("Value of y is : "+subClassObj.y); subClassObj.setY(20); System.out.println("Value of y is : "+subClassObj.y);*/ /* System.out.println("Value of z is : "+subClassObj.z); subClassObj.setZ(30); System.out.println("Value of z is : "+subClassObj.z);*/ System.out.println("Value of z is : " + subClassDiffObj.getZZZ()); /* System.out.println("Value of a is : "+subClassObj.a); subClassObj.setA(20); System.out.println("Value of a is : "+subClassObj.a);*/
DifferentPackage BaseClass Extends SubclassInSamePackage classDifferentPackage Extends SubClassInDifferentPackage
DifferentPackage:2 package pckage1; class BaseClass { public int x = 10; private int y = 10; protected int z = 10; int a = 10; //Implicit Default Access Modifier import pckage1.*; public class ClassInDifferentPackage { public static void main(String args[]) { SubclassInSamePackagesubClassObj = new SubclassInSamePackage(); System.out.println("Value of x is : " + subClassObj.x); subClassObj.setX(30); System.out.println("Value of x is : " + subClassObj.x); /* System.out.println("Value of y is : "+subClassObj.y); subClassObj.setY(20); System.out.println("Value of y is : "+subClassObj.y);*/ /* System.out.println("Value of z is : "+subClassObj.z); subClassObj.setZ(30); System.out.println("Value of z is : "+subClassObj.z);*/ /* System.out.println("Value of a is : "+subClassObj.a); subClassObj.setA(20); System.out.println("Value of a is : "+subClassObj.a);*/