SlideShare a Scribd company logo
Module 05 – Java Package and
Access Control
Danairat T.
Line ID: Danairat
FB: Danairat Thanabodithammachari
+668-1559-1446
Fundamental Java Programming
The Course Outline
Module 01 – Introduction to Java
Module 02 – Basic Java Programming
Module 03 – Control Flow and Exception Handling
Module 04 – Object Oriented in Java
Module 05 – Java Package and Access Control
Module 06 – Java File IO
Module 07 – Java Networking
Module 08 – Java Threading
Module 05 – Java Package and Access Control
• Java Package
• Package declaration
• Importing the package
• Access Modifier
• Same Class access
• Sub Class in the same package
• Sub Class in the difference package
• Difference Class in the difference package
Java Package
Java classes can be grouped together in a namespace called packages.
A package name is the same as the directory (folder) name which contains the
.java files. You declare packages when you define your Java program
Package declaration
The first statement, other than comments, in a Java source file, must be the
package declaration.
Default package. it's possible to omit the package declaration. For small
programs it's common to omit it, in which case Java creates what it calls a
default package. It is recommended that you do not use default packages.
package mypkg01;
import otherpgk.*;
class MyClass01 {
}
Java Package Naming Convention
There is a standard naming convention for packages. Names should
be in lowercase. With small projects that only have a few packages the
names are typically simple (but meaningful!) names:
package pokeranalyzer;
package mycalculator;
In software companies, Naming package is start with the company
domain, before being split into layers or features:
package com.mycompany.utilities;
package org.bobscompany.application.userinterface;
Importing Java Package
To import a specific member into the current class file.
import graphics.Rectangle;
Now you can refer to the Rectangle class by its simple name.
Rectangle myRectangle = new Rectangle();
To import all the types contained in a package, use the import
statement with the asterisk (*) wildcard character.
import graphics.*;
You can refer to any class or interface in the graphics package by
its simple name.
Circle myCircle = new Circle();
Rectangle myRectangle = new Rectangle();
Access Modifier
Variable and Method Member
Access
Modifiers
Same
Class
Class in
the Same
Package
Subclass
In the
same
package
Subclass
In other
package
Other
packages
public Y Y Y Y Y
protected Y Y Y Y N
no access
modifier
Y Y Y N N
private Y N N N N
LAB – Java Package Test
package package1;
class BaseClass {
public int x = 10;
private int y = 10;
protected int z = 10;
int a = 10; //Implicit Default Access Modifier
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
private int getY() {
return y;
}
private void setY(int y) {
this.y = y;
}
protected int getZ() {
return z;
}
protected void setZ(int z) {
this.z = z;
}
int getA() {
return a;
}
void setA(int a) {
this.a = a;
}
}
package package1;
public class SubclassInSamePackage extends BaseClass {
public static void main(String args[]) {
BaseClass rr = new BaseClass();
rr.z = 0;
SubclassInSamePackage subClassObj = new SubclassInSamePackage();
//Access Modifiers - Public
System.out.println("Value of x is : " + subClassObj.x);
subClassObj.setX(20);
System.out.println("Value of x is : " + subClassObj.x);
//Access Modifiers - Private
// If we remove the comments it would result in a compilaton
// error as the fields and methods being accessed are private
/* System.out.println("Value of y is : "+subClassObj.y);
subClassObj.setY(20);
System.out.println("Value of y is : "+subClassObj.y);*/
//Access Modifiers - Protected
System.out.println("Value of z is : " + subClassObj.z);
subClassObj.setZ(30);
System.out.println("Value of z is : " + subClassObj.z);
//Access Modifiers - Default
System.out.println("Value of x is : " + subClassObj.a);
subClassObj.setA(20);
System.out.println("Value of x is : " + subClassObj.a);
}
}
Output
Value of x is : 10
Value of x is : 20
Value of z is : 10
Value of z is : 30
Value of x is : 10
Value of x is : 20
package package2;
import package1.*;
public class SubClassInDifferentPackage extends SubclassInSamePackage {
public int getZZZ() {
return z;
}
public static void main(String args[]) {
SubClassInDifferentPackage subClassDiffObj = new SubClassInDifferentPackage();
SubclassInSamePackage subClassObj = new SubclassInSamePackage();
//Access specifiers - Public
System.out.println("Value of x is : " + subClassObj.x);
subClassObj.setX(30);
System.out.println("Value of x is : " + subClassObj.x);
//Access specifiers - Private
// if we remove the comments it would result in a compilaton
// error as the fields and methods being accessed are private
/* System.out.println("Value of y is : "+subClassObj.y);
subClassObj.setY(20);
System.out.println("Value of y is : "+subClassObj.y);*/
//Access specifiers - Protected
// If we remove the comments it would result in a compilaton
// error as the fields and methods being accessed are protected.
/* 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());
//Access Modifiers - Default
// If we remove the comments it would result in a compilaton
// error as the fields and methods being accessed are default.
/*
System.out.println("Value of a is : "+subClassObj.a);
subClassObj.setA(20);
System.out.println("Value of a is : "+subClassObj.a);*/
}
}
Output
Value of x is : 10
Value of x is : 30
Value of z is : 10
package package2;
import package1.*;
public class ClassInDifferentPackage {
public static void main(String args[]) {
SubclassInSamePackage subClassObj = new SubclassInSamePackage();
//Access Modifiers - Public
System.out.println("Value of x is : " + subClassObj.x);
subClassObj.setX(30);
System.out.println("Value of x is : " + subClassObj.x);
//Access Modifiers - Private
// If we remove the comments it would result in a compilaton
// error as the fields and methods being accessed are private
/* System.out.println("Value of y is : "+subClassObj.y);
subClassObj.setY(20);
System.out.println("Value of y is : "+subClassObj.y);*/
//Access Modifiers - Protected
// If we remove the comments it would result in a compilaton
// error as the fields and methods being accessed are protected.
/* System.out.println("Value of z is : "+subClassObj.z);
subClassObj.setZ(30);
System.out.println("Value of z is : "+subClassObj.z);*/
//Access Modifiers - Default
// If we remove the comments it would result in a compilaton
// error as the fields and methods being accessed are default.
/* System.out.println("Value of a is : "+subClassObj.a);
subClassObj.setA(20);
System.out.println("Value of a is : "+subClassObj.a);*/
}
}
Value of x is : 10
Value of x is : 30
Danairat T.
Line ID: Danairat
FB: Danairat Thanabodithammachari
+668-1559-1446
Thank you

More Related Content

What's hot (20)

PPT
Chapter 1 - An Overview of Computers and Programming Languages
Adan Hubahib
 
PPTX
Three address code In Compiler Design
Shine Raj
 
PPTX
Procedural vs. object oriented programming
Haris Bin Zahid
 
DOCX
Componentes y evolucion del modelado de negocios(investigacion)
Anel Sosa
 
PPTX
Structures in c language
Tanmay Modi
 
PPT
Regular Grammar
Ruchika Sinha
 
PPTX
Storage class in c
kash95
 
PPT
Os Swapping, Paging, Segmentation and Virtual Memory
sgpraju
 
PPT
Compiler1
Natish Kumar
 
PPTX
Algorithm Development
ALI RAZA
 
PPTX
Intermediate code generator
sanchi29
 
PPT
Fundamentals of Programming Chapter 2
Mohd Harris Ahmad Jaal
 
PPTX
Aplicaciones de los lenguajes y autómatas
Claudio Eduardo Manzanero Yermo
 
PPT
Presentation on dbms(relational calculus)
yourbookworldanil
 
PPSX
Java String class
DrRajeshreeKhande
 
PPTX
Macro Processor
Saranya1702
 
PPT
Asymptotic analysis
Soujanya V
 
PPTX
Equivalencia de autómatas finitos y expresiones regulares.
Yamilee Valerio
 
PDF
PL Lecture 04 - Expression and Statement
Schwannden Kuo
 
PPTX
Transaction states and properties
Chetan Mahawar
 
Chapter 1 - An Overview of Computers and Programming Languages
Adan Hubahib
 
Three address code In Compiler Design
Shine Raj
 
Procedural vs. object oriented programming
Haris Bin Zahid
 
Componentes y evolucion del modelado de negocios(investigacion)
Anel Sosa
 
Structures in c language
Tanmay Modi
 
Regular Grammar
Ruchika Sinha
 
Storage class in c
kash95
 
Os Swapping, Paging, Segmentation and Virtual Memory
sgpraju
 
Compiler1
Natish Kumar
 
Algorithm Development
ALI RAZA
 
Intermediate code generator
sanchi29
 
Fundamentals of Programming Chapter 2
Mohd Harris Ahmad Jaal
 
Aplicaciones de los lenguajes y autómatas
Claudio Eduardo Manzanero Yermo
 
Presentation on dbms(relational calculus)
yourbookworldanil
 
Java String class
DrRajeshreeKhande
 
Macro Processor
Saranya1702
 
Asymptotic analysis
Soujanya V
 
Equivalencia de autómatas finitos y expresiones regulares.
Yamilee Valerio
 
PL Lecture 04 - Expression and Statement
Schwannden Kuo
 
Transaction states and properties
Chetan Mahawar
 

Viewers also liked (7)

PPSX
String and string manipulation x
Shahjahan Samoon
 
PDF
Oops (inheritance&interface)
Muthukumaran Subramanian
 
PPT
Lecture09
elearning_portal
 
PDF
Java packages and access specifiers
ashishspace
 
PPTX
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
PPT
Object Oriented Programming with Java
backdoor
 
PPTX
oops concept in java | object oriented programming in java
CPD INDIA
 
String and string manipulation x
Shahjahan Samoon
 
Oops (inheritance&interface)
Muthukumaran Subramanian
 
Lecture09
elearning_portal
 
Java packages and access specifiers
ashishspace
 
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
Object Oriented Programming with Java
backdoor
 
oops concept in java | object oriented programming in java
CPD INDIA
 
Ad

Similar to Java Programming - 05 access control in java (20)

PPTX
Lecture 9 access modifiers and packages
manish kumar
 
PPT
Inheritance and Polymorphism
BG Java EE Course
 
DOCX
Module-4 Java Notes.docx notes about java
KaviShetty
 
PPT
7.Packages and Interfaces(MB).ppt .
happycocoman
 
PPT
Java packages
Raja Sekhar
 
PPTX
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
MaruMengesha
 
PPT
Java access modifiers
Srinivas Reddy
 
PPTX
Packages in java
Jerlin Sundari
 
PPTX
OOPs with Java - Packaging and Access Modifiers
RatnaJava
 
PPT
Javapackages 4th semester
Varendra University Rajshahi-bangladesh
 
PDF
CHAPTER 3 part1.pdf
FacultyAnupamaAlagan
 
DOCX
Class notes(week 7) on packages
Kuntal Bhowmick
 
PPTX
Advance example of Classes and object and programming with case study
MsPariyalNituLaxman
 
PPTX
Java packages oop
Kawsar Hamid Sumon
 
PDF
Java packages
Jeffrey Quevedo
 
PPTX
Lecture 11.pptx galgotias College of engineering and technology
officialpriyanshu228
 
PDF
Chapter 03 enscapsulation
Nurhanna Aziz
 
PPTX
Pj01 x-classes and objects
SasidharaRaoMarrapu
 
PPTX
Chap1 packages
raksharao
 
Lecture 9 access modifiers and packages
manish kumar
 
Inheritance and Polymorphism
BG Java EE Course
 
Module-4 Java Notes.docx notes about java
KaviShetty
 
7.Packages and Interfaces(MB).ppt .
happycocoman
 
Java packages
Raja Sekhar
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_03-Mar-2021_L13...
MaruMengesha
 
Java access modifiers
Srinivas Reddy
 
Packages in java
Jerlin Sundari
 
OOPs with Java - Packaging and Access Modifiers
RatnaJava
 
Javapackages 4th semester
Varendra University Rajshahi-bangladesh
 
CHAPTER 3 part1.pdf
FacultyAnupamaAlagan
 
Class notes(week 7) on packages
Kuntal Bhowmick
 
Advance example of Classes and object and programming with case study
MsPariyalNituLaxman
 
Java packages oop
Kawsar Hamid Sumon
 
Java packages
Jeffrey Quevedo
 
Lecture 11.pptx galgotias College of engineering and technology
officialpriyanshu228
 
Chapter 03 enscapsulation
Nurhanna Aziz
 
Pj01 x-classes and objects
SasidharaRaoMarrapu
 
Chap1 packages
raksharao
 
Ad

More from Danairat Thanabodithammachari (20)

PDF
Thailand State Enterprise - Business Architecture and SE-AM
Danairat Thanabodithammachari
 
PDF
Agile Management
Danairat Thanabodithammachari
 
PDF
Agile Organization and Enterprise Architecture v1129 Danairat
Danairat Thanabodithammachari
 
PDF
Blockchain for Management
Danairat Thanabodithammachari
 
PDF
Enterprise Architecture and Agile Organization Management v1076 Danairat
Danairat Thanabodithammachari
 
PDF
Agile Enterprise Architecture - Danairat
Danairat Thanabodithammachari
 
PDF
Digital Transformation, Enterprise Architecture, Big Data by Danairat
Danairat Thanabodithammachari
 
PDF
Big data Hadoop Analytic and Data warehouse comparison guide
Danairat Thanabodithammachari
 
PDF
Big data hadooop analytic and data warehouse comparison guide
Danairat Thanabodithammachari
 
PDF
Perl for System Automation - 01 Advanced File Processing
Danairat Thanabodithammachari
 
PDF
Perl Programming - 04 Programming Database
Danairat Thanabodithammachari
 
PDF
Perl Programming - 03 Programming File
Danairat Thanabodithammachari
 
PDF
Perl Programming - 02 Regular Expression
Danairat Thanabodithammachari
 
PDF
Perl Programming - 01 Basic Perl
Danairat Thanabodithammachari
 
PDF
Setting up Hadoop YARN Clustering
Danairat Thanabodithammachari
 
PDF
JEE Programming - 03 Model View Controller
Danairat Thanabodithammachari
 
PDF
JEE Programming - 05 JSP
Danairat Thanabodithammachari
 
PDF
JEE Programming - 04 Java Servlets
Danairat Thanabodithammachari
 
PDF
JEE Programming - 08 Enterprise Application Deployment
Danairat Thanabodithammachari
 
PDF
JEE Programming - 07 EJB Programming
Danairat Thanabodithammachari
 
Thailand State Enterprise - Business Architecture and SE-AM
Danairat Thanabodithammachari
 
Agile Organization and Enterprise Architecture v1129 Danairat
Danairat Thanabodithammachari
 
Blockchain for Management
Danairat Thanabodithammachari
 
Enterprise Architecture and Agile Organization Management v1076 Danairat
Danairat Thanabodithammachari
 
Agile Enterprise Architecture - Danairat
Danairat Thanabodithammachari
 
Digital Transformation, Enterprise Architecture, Big Data by Danairat
Danairat Thanabodithammachari
 
Big data Hadoop Analytic and Data warehouse comparison guide
Danairat Thanabodithammachari
 
Big data hadooop analytic and data warehouse comparison guide
Danairat Thanabodithammachari
 
Perl for System Automation - 01 Advanced File Processing
Danairat Thanabodithammachari
 
Perl Programming - 04 Programming Database
Danairat Thanabodithammachari
 
Perl Programming - 03 Programming File
Danairat Thanabodithammachari
 
Perl Programming - 02 Regular Expression
Danairat Thanabodithammachari
 
Perl Programming - 01 Basic Perl
Danairat Thanabodithammachari
 
Setting up Hadoop YARN Clustering
Danairat Thanabodithammachari
 
JEE Programming - 03 Model View Controller
Danairat Thanabodithammachari
 
JEE Programming - 05 JSP
Danairat Thanabodithammachari
 
JEE Programming - 04 Java Servlets
Danairat Thanabodithammachari
 
JEE Programming - 08 Enterprise Application Deployment
Danairat Thanabodithammachari
 
JEE Programming - 07 EJB Programming
Danairat Thanabodithammachari
 

Recently uploaded (20)

PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PPTX
How Cloud Computing is Reinventing Financial Services
Isla Pandora
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
DOCX
Import Data Form Excel to Tally Services
Tally xperts
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
How Cloud Computing is Reinventing Financial Services
Isla Pandora
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Import Data Form Excel to Tally Services
Tally xperts
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 

Java Programming - 05 access control in java

  • 1. Module 05 – Java Package and Access Control Danairat T. Line ID: Danairat FB: Danairat Thanabodithammachari +668-1559-1446
  • 2. Fundamental Java Programming The Course Outline Module 01 – Introduction to Java Module 02 – Basic Java Programming Module 03 – Control Flow and Exception Handling Module 04 – Object Oriented in Java Module 05 – Java Package and Access Control Module 06 – Java File IO Module 07 – Java Networking Module 08 – Java Threading
  • 3. Module 05 – Java Package and Access Control • Java Package • Package declaration • Importing the package • Access Modifier • Same Class access • Sub Class in the same package • Sub Class in the difference package • Difference Class in the difference package
  • 4. Java Package Java classes can be grouped together in a namespace called packages. A package name is the same as the directory (folder) name which contains the .java files. You declare packages when you define your Java program Package declaration The first statement, other than comments, in a Java source file, must be the package declaration. Default package. it's possible to omit the package declaration. For small programs it's common to omit it, in which case Java creates what it calls a default package. It is recommended that you do not use default packages. package mypkg01; import otherpgk.*; class MyClass01 { }
  • 5. Java Package Naming Convention There is a standard naming convention for packages. Names should be in lowercase. With small projects that only have a few packages the names are typically simple (but meaningful!) names: package pokeranalyzer; package mycalculator; In software companies, Naming package is start with the company domain, before being split into layers or features: package com.mycompany.utilities; package org.bobscompany.application.userinterface;
  • 6. Importing Java Package To import a specific member into the current class file. import graphics.Rectangle; Now you can refer to the Rectangle class by its simple name. Rectangle myRectangle = new Rectangle(); To import all the types contained in a package, use the import statement with the asterisk (*) wildcard character. import graphics.*; You can refer to any class or interface in the graphics package by its simple name. Circle myCircle = new Circle(); Rectangle myRectangle = new Rectangle();
  • 7. Access Modifier Variable and Method Member Access Modifiers Same Class Class in the Same Package Subclass In the same package Subclass In other package Other packages public Y Y Y Y Y protected Y Y Y Y N no access modifier Y Y Y N N private Y N N N N
  • 8. LAB – Java Package Test package package1; class BaseClass { public int x = 10; private int y = 10; protected int z = 10; int a = 10; //Implicit Default Access Modifier public int getX() { return x; } public void setX(int x) { this.x = x; } private int getY() { return y; } private void setY(int y) { this.y = y; } protected int getZ() { return z; } protected void setZ(int z) { this.z = z; } int getA() { return a; } void setA(int a) { this.a = a; } }
  • 9. package package1; public class SubclassInSamePackage extends BaseClass { public static void main(String args[]) { BaseClass rr = new BaseClass(); rr.z = 0; SubclassInSamePackage subClassObj = new SubclassInSamePackage(); //Access Modifiers - Public System.out.println("Value of x is : " + subClassObj.x); subClassObj.setX(20); System.out.println("Value of x is : " + subClassObj.x); //Access Modifiers - Private // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are private /* System.out.println("Value of y is : "+subClassObj.y); subClassObj.setY(20); System.out.println("Value of y is : "+subClassObj.y);*/ //Access Modifiers - Protected System.out.println("Value of z is : " + subClassObj.z); subClassObj.setZ(30); System.out.println("Value of z is : " + subClassObj.z); //Access Modifiers - Default System.out.println("Value of x is : " + subClassObj.a); subClassObj.setA(20); System.out.println("Value of x is : " + subClassObj.a); } } Output Value of x is : 10 Value of x is : 20 Value of z is : 10 Value of z is : 30 Value of x is : 10 Value of x is : 20
  • 10. package package2; import package1.*; public class SubClassInDifferentPackage extends SubclassInSamePackage { public int getZZZ() { return z; } public static void main(String args[]) { SubClassInDifferentPackage subClassDiffObj = new SubClassInDifferentPackage(); SubclassInSamePackage subClassObj = new SubclassInSamePackage(); //Access specifiers - Public System.out.println("Value of x is : " + subClassObj.x); subClassObj.setX(30); System.out.println("Value of x is : " + subClassObj.x); //Access specifiers - Private // if we remove the comments it would result in a compilaton // error as the fields and methods being accessed are private /* System.out.println("Value of y is : "+subClassObj.y); subClassObj.setY(20); System.out.println("Value of y is : "+subClassObj.y);*/ //Access specifiers - Protected // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are protected. /* 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()); //Access Modifiers - Default // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are default. /* System.out.println("Value of a is : "+subClassObj.a); subClassObj.setA(20); System.out.println("Value of a is : "+subClassObj.a);*/ } } Output Value of x is : 10 Value of x is : 30 Value of z is : 10
  • 11. package package2; import package1.*; public class ClassInDifferentPackage { public static void main(String args[]) { SubclassInSamePackage subClassObj = new SubclassInSamePackage(); //Access Modifiers - Public System.out.println("Value of x is : " + subClassObj.x); subClassObj.setX(30); System.out.println("Value of x is : " + subClassObj.x); //Access Modifiers - Private // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are private /* System.out.println("Value of y is : "+subClassObj.y); subClassObj.setY(20); System.out.println("Value of y is : "+subClassObj.y);*/ //Access Modifiers - Protected // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are protected. /* System.out.println("Value of z is : "+subClassObj.z); subClassObj.setZ(30); System.out.println("Value of z is : "+subClassObj.z);*/ //Access Modifiers - Default // If we remove the comments it would result in a compilaton // error as the fields and methods being accessed are default. /* System.out.println("Value of a is : "+subClassObj.a); subClassObj.setA(20); System.out.println("Value of a is : "+subClassObj.a);*/ } } Value of x is : 10 Value of x is : 30
  • 12. Danairat T. Line ID: Danairat FB: Danairat Thanabodithammachari +668-1559-1446 Thank you