dieter-chang
Uploaded by
15 SLIDES
280 VUES
150LIKES

Understanding Access Modifiers in Java: Classes, Constructors, Methods, and Fields

DESCRIPTION

This guide explores the concept of access modifiers in Java, focusing on how they control visibility and accessibility of classes, constructors, methods, and fields from other classes. It includes examples of how to use public, private, protected, and default access modifiers within the same package and across different packages. Understand the impact of encapsulation and inheritance in Java through real-world coding scenarios that illustrate access to variables in subclasses.

1 / 15

Télécharger la présentation

Understanding Access Modifiers in Java: Classes, Constructors, Methods, and Fields

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Modifier in Java

  2. Modifier in java

  3. Modifier in Java *Suatuakseske classes, constructors, methods, dan fields. Dapatmengkontrolinformasi yang dapatdiaksesoleh class lain.

  4. Modifier in java: in same package

  5. Modifier in java: in same package BaseClass Extends SubclassInSamePackage

  6. 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();

  7. 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

  8. SubclassDifferent Package

  9. SubclassDifferent Package:1 BaseClass Extends SubclassInSamePackage Extends SubClassInDifferentPackage

  10. 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();

  11. 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);*/

  12. DifferentPackage

  13. DifferentPackage BaseClass Extends SubclassInSamePackage classDifferentPackage Extends SubClassInDifferentPackage

  14. 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);*/

More Related