SlideShare a Scribd company logo
1 of 21
Java Programming
Manish Kumar
(manish87it@gmail.com)
Lecture- 9
Access Modifier &
Package
Contents
 Introduction to java package
 Advantage and Naming Convention of Package
 Creating java package with example
 Different ways to import class
 Creating sub package with example
 Introduction to access modifiers
 Explanation of Access Modifiers with examples
Java package
In java, Package can be defined as a container of classes, sub packages, interfaces. There are two categories of
packages are available in java:
 Built-in package
 User defined package
 Built-in package are those which is already defined such as lang, io, util, net, etc.
 User defined package are those which are create by programmer.
Advantage of java package:
Package is:
• Used to remove naming conflicts because we cannot contain more than one class with same name.
• Also used to achieve data encapsulation (or data-hiding).
• Making searching or locating of classes, interface easier.
Java package
Naming Convention of Package:
The name of a package should be in reverse order of domain names, i.e., college.art.history, college.tech.cse, etc.
Subpackage, those packages that are inside another package. Subpackage are not imported by default, they have
imported explicitly. For example in above art is Subpackage of college.
Creating Package:
We can create package with the help of package keyword. It should be first line in java class.
package <package-name>;
package p1;
Java package
Example: (PackageExample1.java)
package p1;
public class PackageExample1 {
public static void main(String args[]){
System.out.println(“Creating Package.”);
}
}
Output-
Creating Package
Let’s understand how to compile and execute java package
program:
To compile java Package Program (As according above
program) we write following command:
javac –d . PackageExample1.java
javac – It is java compiler.
-d – Specifies the destination where to put generated class file.
Dot (.) – Current directory i.e., If you want to create package
within same directory then use dot otherwise specify the
directory name in place of dot.
To execute the java package program:
java p1.PackageExample1
Java package
Creating Subpackage: (SubPackage.java)
package p1.p2;
public class SubPackage {
public static void main(String args[]){
System.out.println(“Creating Sub Package.”);
}
}
Output-
Creating Sub Package
Java package
To access class from another package:
package p1;
public class PackageExample {
public void display(){
System.out.println(“Package Example”);
}
}
package p2;
import p1.*;
public class PackageExample1 {
public static void main(String args[]) {
PackageExample p = new PackgeExample();
p.display();
}
}
Output:
Package Example
PackageExample.java
PackageExample1.java
Java package
When you want to access class, methods, etc, outside package, then make sure that you declare the class,
method, etc with public keyword. Also import that class, you want to access in other class with import
keyword. Class that is imported does not contain main method so you only compile that program and second
class will be compiling and execute. For Example: (For above)
javac –d . PackageExample.java
javac –d . PackageExample1.java
java p2.PackageExample1
Note:- You can also import with class name as follows:
import p1.PackageExample;
Java package
To access class and package from another package:
There are three ways to access the class and packages from another package:
I. import package.*
II. import package.classname
III. fully qualified name
import package.*
If you are using this method then you can access all the classes and interfaces only i.e. not able to access
Subpackage. With the help import keyword you are able to access classes and interfaces from another package
into current package.
Import package.*
package p1;
class MyPack
{
public void display()
{
System.out.println(“Sparsh Globe.”);
}
}
package p2;
import p1.*;
class MyPack2
{
public static void main(String args[])
{
MyPack mp = new MyPack();
mp.display();
}
}
Output – Sparsh Globe
To execute above program, you save both the class with class name and compile both classes with java compiler
and execute only Mypack2.java class only.
MyPack.java MyPack2.java
Import package.classname
package p1;
class MyPack
{
public void display()
{
System.out.println(“Sparsh Globe.”);
}
}
package p2;
import p1.MyPack;
class MyPack2
{
public static void main(String args[])
{
MyPack mp = new MyPack();
mp.display();
}
}
Output – Sparsh Globe
If you are using this method then you are able to access only declared class of this package.
MyPack.java MyPack2.java
Fully qualified name
package p1;
class MyPack
{
public void display()
{
System.out.println(“Sparsh Globe.”);
}
}
package p2;
class MyPack2
{
public static void main(String args[])
{
P1.MyPack mp = new p1.MyPack();
mp.display();
}
}
Output – Sparsh Globe
In this case, there is no need to import keyword and you can access only declared class of this package. To access
the class or interface using this method every time you write fully qualified name such as package.classname.
MyPack.java MyPack2.java
Java sub package
A subpackage is package inside a package. We create the subpackage to categorize the package again. To
create a subpackage we use the following syntax:
package package.subpackage;
package p1.mypack;
Here p1 is the main package and mypack is treated as subpackage of package p1.
package p1.p2;
class MyPack
{
public static void main(String args[])
{
System.out.println (“Sparsh Globe.”);
}
}
Output – Sparsh Globe
To Compile:
javac -d . Mypack.java
To Run:
java p1.p2.MyPack;
Java package
To send the class files into another directory/drive:
Scenario- Let us suppose I want save my java file (MyPack.java) in p:source and class files (MyPack.class) in
c:classes.
package p1;
class MyPack
{
public static void main(String args[])
{
System.out.println (“Sparsh Globe.”);
}
}
Output – Sparsh Globe
To Compile:
P:source>javac -d c:classes Mypack.java
P:source>set classpath=c:classes;
To Run:
P:source>java p1.MyPack;
Another way to execute
To Compile:
P:source>javac -d c:classes Mypack.java
To Run:
P:source>java –classpath c:classes p1.MyPack;
Access modifier
Access Modifier in java defines the scope/accessibility of class, constructor, method and variables. In java, there
are four types of Access Modifiers:
1. Default
2. Private
3. Protected
4. Public
Access
Modifier
Within Class Within Package
Outside Package
by subclass
Outside Package
Default Y Y N N
Private Y N N N
Protected Y Y Y N
Public Y Y Y Y
Default access modifier
In the case of
Default Access
Modifier, there is
no need to write
any access
modifier before
class, method,
variable, etc. The
access level of
this modifier is
only within the
package.
package pack;
class DefaultModifier{
void display(){
System.out.println("Hello Sparsh");
}
}
package mypack;
import pack.*;
class DefaultModifier1{
public static void main(String args[]){
DefaultModifier dm = new DefaultModifier ();
dm.display();
}
}
In above example, the class DefaultModifier and method
display are default so it cannot be accessed from outside the
package.
DefaultModifier.java DefaultModifier1.java
private access modifier
The access level
of this modifier
is only within the
class where it
declared. We use
private keyword
to make a private
data member,
member
function, class,
etc.
//PrivateData1.java
class PrivateData{
private int data=40;
private void displayMessage (){
System.out.println ("Hello Sparsh");
}}
public class PrivateData1 {
public static void main (String args[]){
PrivateData pd =new PrivateData ();
System.out.println (pd.data);
pd.displayMessage ();
}}
Output –
javac Simple.java
Simple.java:11: error: data has
private access in PrivateData
System.out.println(pd.data);
^
Simple.java:12: error:
displayMessage() has private access
in PrivateData
pd.displayMessage();
^
2 errors
private access modifier
If you make any
class constructor
as private, then
you cannot
create an
instance of that
class from
outside the class.
//PrivateConstructor.java
class PrivateConstructor{
private PrivateConstructor (){}
void display()
{
System.out.println("Hello Sparsh");
}
}
public class PrivateConstructor1 {
public static void main(String args[]){
PrivateConstructor pc=new
PrivateConstructor ();
}
}
Output-
javac Simple.java
Simple.java:7: error: A() has private access
in A
A obj=new A();//Compile Time Error
^
1 error
protected access modifier
The access level of
this modifier is
within the package
and outside the
package (Only if
you accessing
through child
class). We use
protected
keyword to make a
private data
member, member
function, class,
etc.
//ProtectedExample.java
package pack;
public class ProtectedExample
{
protected void display()
{
System.out.println("Hello");
}
}
//ProtectedExample1.java
package mypack;
import pack.*;
class ProtectedExample1 extends ProtectedExample {
public static void main(String args[]){
ProtectedExample1 pe = new ProtectedExample1 ();
pe.display();
}
}
public access modifier
The access level of
this modifier is
everywhere. We
use protected
keyword to make a
private data
member, member
function, class,
etc.
//PublicExample.java
package pack;
public class PublicExample
{
public void display()
{
System.out.println("Hello");
}
}
//PublicExample1.java
package mypack;
import pack.*;
class PublicExample1 extends PublicExample {
public static void main(String args[]){
PublicExample1 pe = new PublicExample1 ();
pe.display();
}
}
Lecture   9 access modifiers and packages

More Related Content

What's hot

Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructorsPraveen M Jigajinni
 
Friend functions
Friend functions Friend functions
Friend functions Megha Singh
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-classDeepak Singh
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismJawad Khan
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism JavaM. Raihan
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - AbstractionMichael Heron
 
Structure of java program diff c- cpp and java
Structure of java program  diff c- cpp and javaStructure of java program  diff c- cpp and java
Structure of java program diff c- cpp and javaMadishetty Prathibha
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONLalitkumar_98
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceOUM SAOKOSAL
 

What's hot (20)

OOP C++
OOP C++OOP C++
OOP C++
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
Friend functions
Friend functions Friend functions
Friend functions
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
Lecture5
Lecture5Lecture5
Lecture5
 
Java interface
Java interfaceJava interface
Java interface
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
Inheritance
InheritanceInheritance
Inheritance
 
Oop in kotlin
Oop in kotlinOop in kotlin
Oop in kotlin
 
Structure of java program diff c- cpp and java
Structure of java program  diff c- cpp and javaStructure of java program  diff c- cpp and java
Structure of java program diff c- cpp and java
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & Interface
 

Similar to Lecture 9 access modifiers and packages

7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .happycocoman
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access ModifiersOOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access ModifiersRatnaJava
 
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptxTHE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptxKavitha713564
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++pankaj chelak
 
Session 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access ModifiersSession 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access ModifiersPawanMM
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers Hitesh-Java
 
Java package
Java packageJava package
Java packageCS_GDRCST
 
Power point presentation on access specifier in OOPs
Power point presentation on access specifier in OOPsPower point presentation on access specifier in OOPs
Power point presentation on access specifier in OOPsAdrizaBera
 

Similar to Lecture 9 access modifiers and packages (20)

7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Packages
PackagesPackages
Packages
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access ModifiersOOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptxTHE PACKAGES CONCEPT  IN JAVA PROGRAMMING.pptx
THE PACKAGES CONCEPT IN JAVA PROGRAMMING.pptx
 
Packages
PackagesPackages
Packages
 
packages in java & c++
packages in java & c++packages in java & c++
packages in java & c++
 
Session 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access ModifiersSession 11 - OOP's with Java - Packaging and Access Modifiers
Session 11 - OOP's with Java - Packaging and Access Modifiers
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
 
Java package
Java packageJava package
Java package
 
Packages and interface
Packages and interfacePackages and interface
Packages and interface
 
Package in Java
Package in JavaPackage in Java
Package in Java
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Java packages
Java packagesJava packages
Java packages
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Power point presentation on access specifier in OOPs
Power point presentation on access specifier in OOPsPower point presentation on access specifier in OOPs
Power point presentation on access specifier in OOPs
 

Recently uploaded

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 

Lecture 9 access modifiers and packages

  • 2. Contents  Introduction to java package  Advantage and Naming Convention of Package  Creating java package with example  Different ways to import class  Creating sub package with example  Introduction to access modifiers  Explanation of Access Modifiers with examples
  • 3. Java package In java, Package can be defined as a container of classes, sub packages, interfaces. There are two categories of packages are available in java:  Built-in package  User defined package  Built-in package are those which is already defined such as lang, io, util, net, etc.  User defined package are those which are create by programmer. Advantage of java package: Package is: • Used to remove naming conflicts because we cannot contain more than one class with same name. • Also used to achieve data encapsulation (or data-hiding). • Making searching or locating of classes, interface easier.
  • 4. Java package Naming Convention of Package: The name of a package should be in reverse order of domain names, i.e., college.art.history, college.tech.cse, etc. Subpackage, those packages that are inside another package. Subpackage are not imported by default, they have imported explicitly. For example in above art is Subpackage of college. Creating Package: We can create package with the help of package keyword. It should be first line in java class. package <package-name>; package p1;
  • 5. Java package Example: (PackageExample1.java) package p1; public class PackageExample1 { public static void main(String args[]){ System.out.println(“Creating Package.”); } } Output- Creating Package Let’s understand how to compile and execute java package program: To compile java Package Program (As according above program) we write following command: javac –d . PackageExample1.java javac – It is java compiler. -d – Specifies the destination where to put generated class file. Dot (.) – Current directory i.e., If you want to create package within same directory then use dot otherwise specify the directory name in place of dot. To execute the java package program: java p1.PackageExample1
  • 6. Java package Creating Subpackage: (SubPackage.java) package p1.p2; public class SubPackage { public static void main(String args[]){ System.out.println(“Creating Sub Package.”); } } Output- Creating Sub Package
  • 7. Java package To access class from another package: package p1; public class PackageExample { public void display(){ System.out.println(“Package Example”); } } package p2; import p1.*; public class PackageExample1 { public static void main(String args[]) { PackageExample p = new PackgeExample(); p.display(); } } Output: Package Example PackageExample.java PackageExample1.java
  • 8. Java package When you want to access class, methods, etc, outside package, then make sure that you declare the class, method, etc with public keyword. Also import that class, you want to access in other class with import keyword. Class that is imported does not contain main method so you only compile that program and second class will be compiling and execute. For Example: (For above) javac –d . PackageExample.java javac –d . PackageExample1.java java p2.PackageExample1 Note:- You can also import with class name as follows: import p1.PackageExample;
  • 9. Java package To access class and package from another package: There are three ways to access the class and packages from another package: I. import package.* II. import package.classname III. fully qualified name import package.* If you are using this method then you can access all the classes and interfaces only i.e. not able to access Subpackage. With the help import keyword you are able to access classes and interfaces from another package into current package.
  • 10. Import package.* package p1; class MyPack { public void display() { System.out.println(“Sparsh Globe.”); } } package p2; import p1.*; class MyPack2 { public static void main(String args[]) { MyPack mp = new MyPack(); mp.display(); } } Output – Sparsh Globe To execute above program, you save both the class with class name and compile both classes with java compiler and execute only Mypack2.java class only. MyPack.java MyPack2.java
  • 11. Import package.classname package p1; class MyPack { public void display() { System.out.println(“Sparsh Globe.”); } } package p2; import p1.MyPack; class MyPack2 { public static void main(String args[]) { MyPack mp = new MyPack(); mp.display(); } } Output – Sparsh Globe If you are using this method then you are able to access only declared class of this package. MyPack.java MyPack2.java
  • 12. Fully qualified name package p1; class MyPack { public void display() { System.out.println(“Sparsh Globe.”); } } package p2; class MyPack2 { public static void main(String args[]) { P1.MyPack mp = new p1.MyPack(); mp.display(); } } Output – Sparsh Globe In this case, there is no need to import keyword and you can access only declared class of this package. To access the class or interface using this method every time you write fully qualified name such as package.classname. MyPack.java MyPack2.java
  • 13. Java sub package A subpackage is package inside a package. We create the subpackage to categorize the package again. To create a subpackage we use the following syntax: package package.subpackage; package p1.mypack; Here p1 is the main package and mypack is treated as subpackage of package p1. package p1.p2; class MyPack { public static void main(String args[]) { System.out.println (“Sparsh Globe.”); } } Output – Sparsh Globe To Compile: javac -d . Mypack.java To Run: java p1.p2.MyPack;
  • 14. Java package To send the class files into another directory/drive: Scenario- Let us suppose I want save my java file (MyPack.java) in p:source and class files (MyPack.class) in c:classes. package p1; class MyPack { public static void main(String args[]) { System.out.println (“Sparsh Globe.”); } } Output – Sparsh Globe To Compile: P:source>javac -d c:classes Mypack.java P:source>set classpath=c:classes; To Run: P:source>java p1.MyPack; Another way to execute To Compile: P:source>javac -d c:classes Mypack.java To Run: P:source>java –classpath c:classes p1.MyPack;
  • 15. Access modifier Access Modifier in java defines the scope/accessibility of class, constructor, method and variables. In java, there are four types of Access Modifiers: 1. Default 2. Private 3. Protected 4. Public Access Modifier Within Class Within Package Outside Package by subclass Outside Package Default Y Y N N Private Y N N N Protected Y Y Y N Public Y Y Y Y
  • 16. Default access modifier In the case of Default Access Modifier, there is no need to write any access modifier before class, method, variable, etc. The access level of this modifier is only within the package. package pack; class DefaultModifier{ void display(){ System.out.println("Hello Sparsh"); } } package mypack; import pack.*; class DefaultModifier1{ public static void main(String args[]){ DefaultModifier dm = new DefaultModifier (); dm.display(); } } In above example, the class DefaultModifier and method display are default so it cannot be accessed from outside the package. DefaultModifier.java DefaultModifier1.java
  • 17. private access modifier The access level of this modifier is only within the class where it declared. We use private keyword to make a private data member, member function, class, etc. //PrivateData1.java class PrivateData{ private int data=40; private void displayMessage (){ System.out.println ("Hello Sparsh"); }} public class PrivateData1 { public static void main (String args[]){ PrivateData pd =new PrivateData (); System.out.println (pd.data); pd.displayMessage (); }} Output – javac Simple.java Simple.java:11: error: data has private access in PrivateData System.out.println(pd.data); ^ Simple.java:12: error: displayMessage() has private access in PrivateData pd.displayMessage(); ^ 2 errors
  • 18. private access modifier If you make any class constructor as private, then you cannot create an instance of that class from outside the class. //PrivateConstructor.java class PrivateConstructor{ private PrivateConstructor (){} void display() { System.out.println("Hello Sparsh"); } } public class PrivateConstructor1 { public static void main(String args[]){ PrivateConstructor pc=new PrivateConstructor (); } } Output- javac Simple.java Simple.java:7: error: A() has private access in A A obj=new A();//Compile Time Error ^ 1 error
  • 19. protected access modifier The access level of this modifier is within the package and outside the package (Only if you accessing through child class). We use protected keyword to make a private data member, member function, class, etc. //ProtectedExample.java package pack; public class ProtectedExample { protected void display() { System.out.println("Hello"); } } //ProtectedExample1.java package mypack; import pack.*; class ProtectedExample1 extends ProtectedExample { public static void main(String args[]){ ProtectedExample1 pe = new ProtectedExample1 (); pe.display(); } }
  • 20. public access modifier The access level of this modifier is everywhere. We use protected keyword to make a private data member, member function, class, etc. //PublicExample.java package pack; public class PublicExample { public void display() { System.out.println("Hello"); } } //PublicExample1.java package mypack; import pack.*; class PublicExample1 extends PublicExample { public static void main(String args[]){ PublicExample1 pe = new PublicExample1 (); pe.display(); } }