SlideShare a Scribd company logo
1 of 31
Core Java Training
OOPs with Java Contd.
Page 1Classification: Restricted
Agenda
• Packaging Classes & Access Modifiers
Java & JEE Training
Review of OOPs concepts (Last class)
Page 3Classification: Restricted
Review…
• Difference between interfaces and abstract classes?
• When to use which?
• Multiple inheritance by implementing multiple interfaces is allowed. Why?
Does it not cause ambiguity?
Page 4Classification: Restricted
Point to ponder…
• Can we override static methods?
Page 5Classification: Restricted
Point to ponder…
• Can we override static methods? No
Page 6Classification: Restricted
Point to ponder…
• Can we override static methods? No
• What is Method Hiding? How is it different from Method Overriding?
Java & JEE Training
Packaging classes in Java
Page 8Classification: Restricted
Java Package
• 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
Page 9Classification: Restricted
Java Package
Page 10Classification: Restricted
Java Package Example
package mypack;
public class Simple{
public static void main(String args[]){
System.out.println("Welcome to package");
}
}
Page 11Classification: Restricted
Accessing package
There are three ways to access the package from outside the package.
• import package.*;
• import package.classname;
• fully qualified name.
Page 12Classification: Restricted
Accessing package: Using packagename.*
• 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();
}
}
Page 13Classification: Restricted
Accessing package: Using packagename.classname
• 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();
}
}
Page 14Classification: Restricted
• 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 qualified name
obj.msg();
}
}
Accessing package: Using fully qualified name
Page 15Classification: Restricted
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.
Page 16Classification: Restricted
More rules…
• 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{}
Page 17Classification: Restricted
How to put 2 public classes in a package?
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{}
Page 18Classification: Restricted
Static import
• 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");
}
}
Page 19Classification: Restricted
Access Modifiers
Page 20Classification: Restricted
Private access modifier
• 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
}
}
Page 21Classification: Restricted
Private constructor example
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
}
}
Page 22Classification: Restricted
Can a class be private?
Rule: A class cannot be private or protected except nested class.
Page 23Classification: Restricted
Default access modifier
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
}
}
Page 24Classification: Restricted
protected access modifier
• 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();
}
}
Page 25Classification: Restricted
public access modifier
• 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();
}
}
Page 26Classification: Restricted
Overriding rule…
• 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();
}
}
Page 27Classification: Restricted
Encapsulation in Java
• 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.
Page 28Classification: Restricted
Java Bean Example (Best Practice)
//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
}
}
Page 29Classification: Restricted
Topics to be covered in next session
• Exception Handling
• Exception Class hierarchy
• Types of Exception
• Keywords for Exception Handling
Page 30Classification: Restricted
Thank you!

More Related Content

What's hot

Review Session - Part -2
Review Session - Part -2Review Session - Part -2
Review Session - Part -2Hitesh-Java
 
Session 02 - Elements of Java Language
Session 02 - Elements of Java LanguageSession 02 - Elements of Java Language
Session 02 - Elements of Java LanguagePawanMM
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+javaYe Win
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 
Elements of Java Language - Continued
Elements of Java Language - Continued Elements of Java Language - Continued
Elements of Java Language - Continued Hitesh-Java
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1Sherihan Anver
 
Most Asked Java Interview Question and Answer
Most Asked Java Interview Question and AnswerMost Asked Java Interview Question and Answer
Most Asked Java Interview Question and AnswerTOPS Technologies
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2Sherihan Anver
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiersKhaled Adnan
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)Prof. Erwin Globio
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingPurvik Rana
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiersSrinivas Reddy
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobGaruda Trainings
 

What's hot (20)

Review Session - Part -2
Review Session - Part -2Review Session - Part -2
Review Session - Part -2
 
Session 02 - Elements of Java Language
Session 02 - Elements of Java LanguageSession 02 - Elements of Java Language
Session 02 - Elements of Java Language
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Elements of Java Language - Continued
Elements of Java Language - Continued Elements of Java Language - Continued
Elements of Java Language - Continued
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
 
Most Asked Java Interview Question and Answer
Most Asked Java Interview Question and AnswerMost Asked Java Interview Question and Answer
Most Asked Java Interview Question and Answer
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
JAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - MultithreadingJAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - Multithreading
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O
 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
 
OOP in Java
OOP in JavaOOP in Java
OOP in Java
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
 
Java non access modifiers
Java non access modifiersJava non access modifiers
Java non access modifiers
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
 

Similar to OOPs with Java - Packaging and Access Modifiers

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

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
 
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 in java
Packages in javaPackages in java
Packages in java
 
7.Packages and Interfaces(MB).ppt .
7.Packages and Interfaces(MB).ppt             .7.Packages and Interfaces(MB).ppt             .
7.Packages and Interfaces(MB).ppt .
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
 
Unit 2 notes.pdf
Unit 2 notes.pdfUnit 2 notes.pdf
Unit 2 notes.pdf
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
OOP_packages_222902019.pptx
OOP_packages_222902019.pptxOOP_packages_222902019.pptx
OOP_packages_222902019.pptx
 
OOP_packages_222902019.pptx
OOP_packages_222902019.pptxOOP_packages_222902019.pptx
OOP_packages_222902019.pptx
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Packages
PackagesPackages
Packages
 
Packages
PackagesPackages
Packages
 
Java packages oop
Java packages oopJava packages oop
Java packages oop
 
Packages in java
Packages in javaPackages in java
Packages in java
 
packages.ppt
packages.pptpackages.ppt
packages.ppt
 
Packages
PackagesPackages
Packages
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 

More from Hitesh-Java

Spring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVCSpring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVCHitesh-Java
 
Spring - Part 3 - AOP
Spring - Part 3 - AOPSpring - Part 3 - AOP
Spring - Part 3 - AOPHitesh-Java
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesSpring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesHitesh-Java
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Hitesh-Java
 
JSP - Part 2 (Final)
JSP - Part 2 (Final) JSP - Part 2 (Final)
JSP - Part 2 (Final) Hitesh-Java
 
Struts 2 - Hibernate Integration
Struts 2 - Hibernate Integration Struts 2 - Hibernate Integration
Struts 2 - Hibernate Integration Hitesh-Java
 
Struts 2 - Introduction
Struts 2 - Introduction Struts 2 - Introduction
Struts 2 - Introduction Hitesh-Java
 
Hibernate - Part 2
Hibernate - Part 2 Hibernate - Part 2
Hibernate - Part 2 Hitesh-Java
 
Hibernate - Part 1
Hibernate - Part 1Hibernate - Part 1
Hibernate - Part 1Hitesh-Java
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization Hitesh-Java
 
Collections - Maps
Collections - Maps Collections - Maps
Collections - Maps Hitesh-Java
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets Hitesh-Java
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Hitesh-Java
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List Hitesh-Java
 
Exception Handling - Part 1
Exception Handling - Part 1 Exception Handling - Part 1
Exception Handling - Part 1 Hitesh-Java
 
Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java Hitesh-Java
 

More from Hitesh-Java (20)

Spring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVCSpring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVC
 
Spring - Part 3 - AOP
Spring - Part 3 - AOPSpring - Part 3 - AOP
Spring - Part 3 - AOP
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesSpring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
 
JSP - Part 2 (Final)
JSP - Part 2 (Final) JSP - Part 2 (Final)
JSP - Part 2 (Final)
 
JSP - Part 1
JSP - Part 1JSP - Part 1
JSP - Part 1
 
Struts 2 - Hibernate Integration
Struts 2 - Hibernate Integration Struts 2 - Hibernate Integration
Struts 2 - Hibernate Integration
 
Struts 2 - Introduction
Struts 2 - Introduction Struts 2 - Introduction
Struts 2 - Introduction
 
Hibernate - Part 2
Hibernate - Part 2 Hibernate - Part 2
Hibernate - Part 2
 
Hibernate - Part 1
Hibernate - Part 1Hibernate - Part 1
Hibernate - Part 1
 
JDBC Part - 2
JDBC Part - 2JDBC Part - 2
JDBC Part - 2
 
JDBC
JDBCJDBC
JDBC
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization
 
Collections - Maps
Collections - Maps Collections - Maps
Collections - Maps
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
 
Object Class
Object Class Object Class
Object Class
 
Exception Handling - Part 1
Exception Handling - Part 1 Exception Handling - Part 1
Exception Handling - Part 1
 
Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java
 

Recently uploaded

Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringWSO2
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaWSO2
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceIES VE
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...caitlingebhard1
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 

Recently uploaded (20)

Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

OOPs with Java - Packaging and Access Modifiers

  • 1. Core Java Training OOPs with Java Contd.
  • 2. Page 1Classification: Restricted Agenda • Packaging Classes & Access Modifiers
  • 3. Java & JEE Training Review of OOPs concepts (Last class)
  • 4. Page 3Classification: Restricted Review… • Difference between interfaces and abstract classes? • When to use which? • Multiple inheritance by implementing multiple interfaces is allowed. Why? Does it not cause ambiguity?
  • 5. Page 4Classification: Restricted Point to ponder… • Can we override static methods?
  • 6. Page 5Classification: Restricted Point to ponder… • Can we override static methods? No
  • 7. Page 6Classification: Restricted Point to ponder… • Can we override static methods? No • What is Method Hiding? How is it different from Method Overriding?
  • 8. Java & JEE Training Packaging classes in Java
  • 9. Page 8Classification: Restricted Java Package • 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
  • 11. Page 10Classification: Restricted Java Package Example package mypack; public class Simple{ public static void main(String args[]){ System.out.println("Welcome to package"); } }
  • 12. Page 11Classification: Restricted Accessing package There are three ways to access the package from outside the package. • import package.*; • import package.classname; • fully qualified name.
  • 13. Page 12Classification: Restricted Accessing package: Using packagename.* • 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(); } }
  • 14. Page 13Classification: Restricted Accessing package: Using packagename.classname • 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(); } }
  • 15. Page 14Classification: Restricted • 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 qualified name obj.msg(); } } Accessing package: Using fully qualified name
  • 16. Page 15Classification: Restricted 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.
  • 17. Page 16Classification: Restricted More rules… • 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{}
  • 18. Page 17Classification: Restricted How to put 2 public classes in a package? 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{}
  • 19. Page 18Classification: Restricted Static import • 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"); } }
  • 21. Page 20Classification: Restricted Private access modifier • 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 } }
  • 22. Page 21Classification: Restricted Private constructor example 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 } }
  • 23. Page 22Classification: Restricted Can a class be private? Rule: A class cannot be private or protected except nested class.
  • 24. Page 23Classification: Restricted Default access modifier 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 } }
  • 25. Page 24Classification: Restricted protected access modifier • 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(); } }
  • 26. Page 25Classification: Restricted public access modifier • 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(); } }
  • 27. Page 26Classification: Restricted Overriding rule… • 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(); } }
  • 28. Page 27Classification: Restricted Encapsulation in Java • 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.
  • 29. Page 28Classification: Restricted Java Bean Example (Best Practice) //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 } }
  • 30. Page 29Classification: Restricted Topics to be covered in next session • Exception Handling • Exception Class hierarchy • Types of Exception • Keywords for Exception Handling