SlideShare a Scribd company logo
Java/J2EE Programming Training
OOPs with Java Contd.
Page 2Classification: Restricted
Agenda
• Packaging Classes & Access Modifiers
Java Package
Classification: Restricted Page 8
• A java package is a group of similar types of classes, interfaces and sub-
packages.
• Package in java can be categorized in two form, built-in package and user-
defined package.
• There are many built-in packages such as java, lang, awt, javax, swing,
net, io, util, sql etc.
• Advantages
oJava package is used to categorize the classes and interfaces so that
they can be easily maintained.
oJava package provides access protection.
oJava package removes naming collision
Java Package
Classification: Restricted Page 9
Java Package Example
Classification: Restricted Page 10
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
Accessing package
Classification: Restricted Page 11
There are three ways to access the package from outside the package.
• import package.*;
• import package.classname;
• fully qualified name.
Accessing package: Using packagename.*
Classification: Restricted Page 12
• If you use package.* then all the classes and interfaces of this package will
be accessible but not subpackages.
• The import keyword is used to make the classes and interface of another package
accessible to the current package.
//save as A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save as B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){ A
obj = new A();
obj.msg();
}
}
Accessing package: Using packagename.classname
Classification: Restricted Page 13
• If you import package.classname then only declared class of this package
will be accessible.
//save as A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save as B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){ A
obj = new A();
obj.msg();
}
}
• If you use fully qualified name then only declared class of this package will be accessible. Now there
is no need to import. But you need to use fully qualified name every time when you are accessing
the class or interface.
• It is generally used when two packages have same class name e.g. java.util and java.sql packages
contain Date class.
//save by A.java package
pack; public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java package
mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualifiedname
obj.msg();
}
}
Classification: Restricted Page 14
Accessing package: Using fully qualified name
Packages.. rules
• If you import a package, subpackages will not be imported.
• If you import a package, all the classes and interface of that package
will be imported excluding the classes and interfaces of the
subpackages. Hence, you need to import the subpackage as well.
• Java program structure
• The standard of defining package is domain.company.package e.g.
com.mindsmapped.bean or org.wikipedia.dao.
Classification: Restricted Page 15
More rules…
Classification: Restricted Page 16
• There can be only one public class in a java source file and it must
be saved by the public class name.
//save as C.java otherwise Compile Time Error
class A{}
class B{}
public class C{}
How to put 2 public classes in a package?
Classification: Restricted Page 17
If you want to put two public classes in a package, have two java source files
containing one public class, but keep the package name same.
//save as A.java
package dummy;
public class A{}
//save as B.java
package dummy;
public class B{}
Static import
Classification: Restricted Page 18
• The import allows the java programmer to access classes of a package
without package qualification whereas the static import feature allows
to access the static members of a class without the class qualification
import static java.lang.System.*;
class StaticImportExample{
public static void main(String args[]){
out.println("Hello"); //Now no need of System.out
out.println("Java");
}
}
Access Modifiers
Classification: Restricted Page 19
Private access modifier
Classification: Restricted Page 20
• The private access modifier is accessible only within class.
class A{
private int data=40;
private void msg(){System.out.println("Hello java");}
}
public class Simple{
public static void main(String args[]){
A obj=new A();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}
Private constructor example
Classification: Restricted Page 21
class A{
private A(){}//private constructor
void msg(){System.out.println("Hello java");}
}
public class Simple{
public static void main(String args[]){
A obj=new A();//Compile Time Error
}
}
Can a class be private?
Classification: Restricted Page 22
Rule:A class cannot be private or protected except nested class.
Default access modifier
Classification: Restricted Page 23
If you don't use any modifier, it is treated as default bydefault. The default
modifier is accessible only within package.
//save by A.java
package pack;
class A{
void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A(); //Compile Time
Error obj.msg(); //Compile Time
Error
}
}
protected access modifier
Classification: Restricted Page 24
• The protected access modifier is accessible within package and outside
the package but through inheritance only.
• The protected access modifier can be applied on the data member, method and
constructor. It can't be applied on the class.
//save by A.java
package pack;
public class A{
protected void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
public access modifier
Classification: Restricted Page 25
• The public access modifier is accessible everywhere. It has the widest scope
among all other modifiers.
//save as A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save as B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Overriding rule…
Classification: Restricted Page 26
• If you are overriding any method, overriding method (i.e. declared in
subclass) must not be more restrictive.
class A{
protected void msg(){System.out.println("Hello java");}
}
public class Simple extends A{
void msg(){System.out.println("Hello java");}//Compile-
time error public static void main(String args[]){
Simple obj=new
Simple(); obj.msg();
}
}
Encapsulation in Java
Classification: Restricted Page 27
• Encapsulation in java is a process of wrapping code and data together
into a single unit, for example capsule i.e. mixed of several medicines.
• The Java Bean class is the example of fully encapsulated class.
• Advantages:
• By providing only setter or getter method, you can make the
class read-only or write-only.
• It provides you the control over the data. Suppose you want to set the
value of id i.e. greater than 100 only, you can write the logic inside the
setter method.
Java Bean Example (Best Practice)
Classification: Restricted Page 28
//save as Student.java
package com.dummy;
public class Student{
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name=name
}
}
Thank You
Ad

More Related Content

Similar to OOPs with Java - Packaging and Access Modifiers (20)

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
 
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
PawanMM
 
Packages in java
Packages in javaPackages in java
Packages in java
Kavitha713564
 
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
Kavitha713564
 
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
 
PACKAGE.PPT12345678912345949745654646455
PACKAGE.PPT12345678912345949745654646455PACKAGE.PPT12345678912345949745654646455
PACKAGE.PPT12345678912345949745654646455
meetjaju38
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
manish kumar
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
GayathriRHICETCSESTA
 
Packages
PackagesPackages
Packages
Monika Mishra
 
Packages
PackagesPackages
Packages
Nuha Noor
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
Khaled Adnan
 
Packages in java
Packages in javaPackages in java
Packages in java
Jerlin Sundari
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
Prognoz Technologies Pvt. Ltd.
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
Varendra University Rajshahi-bangladesh
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
radhika477746
 
packages in java programming language ppt
packages in java programming language pptpackages in java programming language ppt
packages in java programming language ppt
ssuser5d6130
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
surajthakur474818
 
Objects and classes in OO Programming concepts
Objects and classes in OO Programming conceptsObjects and classes in OO Programming concepts
Objects and classes in OO Programming concepts
researchveltech
 
Packages and interface
Packages and interfacePackages and interface
Packages and interface
KarthigaGunasekaran1
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
Kawsar Hamid Sumon
 
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
 
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
PawanMM
 
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
Kavitha713564
 
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
 
PACKAGE.PPT12345678912345949745654646455
PACKAGE.PPT12345678912345949745654646455PACKAGE.PPT12345678912345949745654646455
PACKAGE.PPT12345678912345949745654646455
meetjaju38
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
manish kumar
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
Khaled Adnan
 
packages in java programming language ppt
packages in java programming language pptpackages in java programming language ppt
packages in java programming language ppt
ssuser5d6130
 
Objects and classes in OO Programming concepts
Objects and classes in OO Programming conceptsObjects and classes in OO Programming concepts
Objects and classes in OO Programming concepts
researchveltech
 

More from RatnaJava (14)

Review Session and Attending Java Interviews
Review Session and Attending Java InterviewsReview Session and Attending Java Interviews
Review Session and Attending Java Interviews
RatnaJava
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & sets
RatnaJava
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing BasicsCollections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics
RatnaJava
 
Collections Array list
Collections Array listCollections Array list
Collections Array list
RatnaJava
 
Object Class
Object ClassObject Class
Object Class
RatnaJava
 
Exception Handling
Exception Handling  Exception Handling
Exception Handling
RatnaJava
 
OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesOOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and Interfaces
RatnaJava
 
OOP with Java - Part 3
OOP with Java - Part 3OOP with Java - Part 3
OOP with Java - Part 3
RatnaJava
 
OOP with Java - continued
OOP with Java - continuedOOP with Java - continued
OOP with Java - continued
RatnaJava
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
RatnaJava
 
Data Handling and Function
Data Handling and FunctionData Handling and Function
Data Handling and Function
RatnaJava
 
Introduction to Java Part-3
Introduction to Java Part-3Introduction to Java Part-3
Introduction to Java Part-3
RatnaJava
 
Introduction to Java Part-2
Introduction to Java Part-2Introduction to Java Part-2
Introduction to Java Part-2
RatnaJava
 
Introduction to Java
Introduction to Java Introduction to Java
Introduction to Java
RatnaJava
 
Review Session and Attending Java Interviews
Review Session and Attending Java InterviewsReview Session and Attending Java Interviews
Review Session and Attending Java Interviews
RatnaJava
 
Collections - Lists & sets
Collections - Lists & setsCollections - Lists & sets
Collections - Lists & sets
RatnaJava
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing BasicsCollections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics
RatnaJava
 
Collections Array list
Collections Array listCollections Array list
Collections Array list
RatnaJava
 
Object Class
Object ClassObject Class
Object Class
RatnaJava
 
Exception Handling
Exception Handling  Exception Handling
Exception Handling
RatnaJava
 
OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesOOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and Interfaces
RatnaJava
 
OOP with Java - Part 3
OOP with Java - Part 3OOP with Java - Part 3
OOP with Java - Part 3
RatnaJava
 
OOP with Java - continued
OOP with Java - continuedOOP with Java - continued
OOP with Java - continued
RatnaJava
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
RatnaJava
 
Data Handling and Function
Data Handling and FunctionData Handling and Function
Data Handling and Function
RatnaJava
 
Introduction to Java Part-3
Introduction to Java Part-3Introduction to Java Part-3
Introduction to Java Part-3
RatnaJava
 
Introduction to Java Part-2
Introduction to Java Part-2Introduction to Java Part-2
Introduction to Java Part-2
RatnaJava
 
Introduction to Java
Introduction to Java Introduction to Java
Introduction to Java
RatnaJava
 
Ad

Recently uploaded (20)

Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
James Anderson
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
Q1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor PresentationQ1 2025 Dropbox Earnings and Investor Presentation
Q1 2025 Dropbox Earnings and Investor Presentation
Dropbox
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent LasterAI 3-in-1: Agents, RAG, and Local Models - Brent Laster
AI 3-in-1: Agents, RAG, and Local Models - Brent Laster
All Things Open
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Viam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdfViam product demo_ Deploying and scaling AI with hardware.pdf
Viam product demo_ Deploying and scaling AI with hardware.pdf
camilalamoratta
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
James Anderson
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
Ad

OOPs with Java - Packaging and Access Modifiers

  • 2. Page 2Classification: Restricted Agenda • Packaging Classes & Access Modifiers
  • 3. Java Package Classification: Restricted Page 8 • A java package is a group of similar types of classes, interfaces and sub- packages. • Package in java can be categorized in two form, built-in package and user- defined package. • There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. • Advantages oJava package is used to categorize the classes and interfaces so that they can be easily maintained. oJava package provides access protection. oJava package removes naming collision
  • 5. Java Package Example Classification: Restricted Page 10 package mypack; public class Simple{ public static void main(String args[]){ System.out.println("Welcome to package"); } }
  • 6. Accessing package Classification: Restricted Page 11 There are three ways to access the package from outside the package. • import package.*; • import package.classname; • fully qualified name.
  • 7. Accessing package: Using packagename.* Classification: Restricted Page 12 • If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. • The import keyword is used to make the classes and interface of another package accessible to the current package. //save as A.java package pack; public class A{ public void msg(){System.out.println("Hello");} } //save as B.java package mypack; import pack.*; class B{ public static void main(String args[]){ A obj = new A(); obj.msg(); } }
  • 8. Accessing package: Using packagename.classname Classification: Restricted Page 13 • If you import package.classname then only declared class of this package will be accessible. //save as A.java package pack; public class A{ public void msg(){System.out.println("Hello");} } //save as B.java package mypack; import pack.A; class B{ public static void main(String args[]){ A obj = new A(); obj.msg(); } }
  • 9. • If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface. • It is generally used when two packages have same class name e.g. java.util and java.sql packages contain Date class. //save by A.java package pack; public class A{ public void msg(){System.out.println("Hello");} } //save by B.java package mypack; class B{ public static void main(String args[]){ pack.A obj = new pack.A();//using fully qualifiedname obj.msg(); } } Classification: Restricted Page 14 Accessing package: Using fully qualified name
  • 10. Packages.. rules • If you import a package, subpackages will not be imported. • If you import a package, all the classes and interface of that package will be imported excluding the classes and interfaces of the subpackages. Hence, you need to import the subpackage as well. • Java program structure • The standard of defining package is domain.company.package e.g. com.mindsmapped.bean or org.wikipedia.dao. Classification: Restricted Page 15
  • 11. More rules… Classification: Restricted Page 16 • There can be only one public class in a java source file and it must be saved by the public class name. //save as C.java otherwise Compile Time Error class A{} class B{} public class C{}
  • 12. How to put 2 public classes in a package? Classification: Restricted Page 17 If you want to put two public classes in a package, have two java source files containing one public class, but keep the package name same. //save as A.java package dummy; public class A{} //save as B.java package dummy; public class B{}
  • 13. Static import Classification: Restricted Page 18 • The import allows the java programmer to access classes of a package without package qualification whereas the static import feature allows to access the static members of a class without the class qualification import static java.lang.System.*; class StaticImportExample{ public static void main(String args[]){ out.println("Hello"); //Now no need of System.out out.println("Java"); } }
  • 15. Private access modifier Classification: Restricted Page 20 • The private access modifier is accessible only within class. class A{ private int data=40; private void msg(){System.out.println("Hello java");} } public class Simple{ public static void main(String args[]){ A obj=new A(); System.out.println(obj.data);//Compile Time Error obj.msg();//Compile Time Error } }
  • 16. Private constructor example Classification: Restricted Page 21 class A{ private A(){}//private constructor void msg(){System.out.println("Hello java");} } public class Simple{ public static void main(String args[]){ A obj=new A();//Compile Time Error } }
  • 17. Can a class be private? Classification: Restricted Page 22 Rule:A class cannot be private or protected except nested class.
  • 18. Default access modifier Classification: Restricted Page 23 If you don't use any modifier, it is treated as default bydefault. The default modifier is accessible only within package. //save by A.java package pack; class A{ void msg(){System.out.println("Hello");} } //save by B.java package mypack; import pack.*; class B{ public static void main(String args[]){ A obj = new A(); //Compile Time Error obj.msg(); //Compile Time Error } }
  • 19. protected access modifier Classification: Restricted Page 24 • The protected access modifier is accessible within package and outside the package but through inheritance only. • The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class. //save by A.java package pack; public class A{ protected void msg(){System.out.println("Hello");} } //save by B.java package mypack; import pack.*; class B extends A{ public static void main(String args[]){ B obj = new B(); obj.msg(); } }
  • 20. public access modifier Classification: Restricted Page 25 • The public access modifier is accessible everywhere. It has the widest scope among all other modifiers. //save as A.java package pack; public class A{ public void msg(){System.out.println("Hello");} } //save as B.java package mypack; import pack.*; class B{ public static void main(String args[]){ A obj = new A(); obj.msg(); } }
  • 21. Overriding rule… Classification: Restricted Page 26 • If you are overriding any method, overriding method (i.e. declared in subclass) must not be more restrictive. class A{ protected void msg(){System.out.println("Hello java");} } public class Simple extends A{ void msg(){System.out.println("Hello java");}//Compile- time error public static void main(String args[]){ Simple obj=new Simple(); obj.msg(); } }
  • 22. Encapsulation in Java Classification: Restricted Page 27 • Encapsulation in java is a process of wrapping code and data together into a single unit, for example capsule i.e. mixed of several medicines. • The Java Bean class is the example of fully encapsulated class. • Advantages: • By providing only setter or getter method, you can make the class read-only or write-only. • It provides you the control over the data. Suppose you want to set the value of id i.e. greater than 100 only, you can write the logic inside the setter method.
  • 23. Java Bean Example (Best Practice) Classification: Restricted Page 28 //save as Student.java package com.dummy; public class Student{ private String name; public String getName(){ return name; } public void setName(String name){ this.name=name } }
  翻译: