KEMBAR78
Java Programming - 05 access control in java | PDF
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

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 TheCourse 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 classescan 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 NamingConvention 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 Toimport 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 andMethod 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 โ€“ JavaPackage 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 classSubclassInSamePackage 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.*; publicclass 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.*; publicclass 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