SlideShare a Scribd company logo
Java/J2EE Programming Training
Access Specifier
Page 1Classification: Restricted
Agenda
• Declarations and Access Control
• Class Modifiers
• Access Modifiers for Members
• Non-Access Modifiers for Members
• More Member Modifiers
• Modifiers for Inner Classes
• Class Declarations
• Interface Declarations
• Implementing an Interface
• main() Method
• Extra points to remember
Declarations and Access
Control
Page 3Classification: Restricted
Objective
• Declare classes, nested classes, methods, instance variables, static variables
and automatic (method local) variables making appropriate use of all
permitted modifiers (such as public, final, static, abstract, etc.). State the
significance of each of these modifiers both singly and in combination and
state the effect of package relationships on declared items qualified by
these modifiers.
Page 4Classification: Restricted
Objective
•Identify correctly constructed package declarations, import
statements, class declarations (of all forms including inner
classes) interface declarations, method declarations (including
the main method that is used to start execution of a class),
variable declarations, and identifiers.
•Identify classes that correctly implement an interface where
that interface is either java.lang.Runnable or a fully specified
interface in the question
Page 5Classification: Restricted
Class Modifiers
• Access Modifiers for Classes
Top level classes and interfaces can be specified as public. If no access
modifier is specified, the access level is default access
• Other Modifiers for Classes
abstract – The class cannot be instantiated
final - The class cannot be extended ie; it cannot be subclassed
Page 6Classification: Restricted
Access Modifiers for Members
• private - can only be accessed from inside the class.
• protected - can only be accessed by classes in the same package or
subclasses of this class
• public - can be accessed by any other class.
• If there is no access modifier specified- the members have default access
level ie; they can be accessed only by other classes in the same package
Page 7Classification: Restricted
Non-Access Modifiers for Members
• synchronized – only for methods. Can be accessed only by one thread at a
time.
• transient – only for variables. Cannot be serialized.
• abstract – only for methods. Method is not implemented. Has to be
implemented by the first concrete subclass
• final – For methods, it means that the method cannot be inherited. For
variables, it makes the variable a constant.
Page 8Classification: Restricted
More Member Modifiers
• native – only for methods, means that the method is not written in Java,
but in a native language.
• strictfp – only for methods and classes. Forces floating points to adhere to
IEE754 standard.
• volatile – only for variables. The compiler will not perform optimization on
the variables if this is specified.
• static – for variables and methods. Static variables are shared by all
instances of the class. Static methods and variables can be used without
having any instances of that class at all.
Page 9Classification: Restricted
Modifiers for Inner Classes
• Modifiers which can be applied to non-static inner classes are final,
abstract, public and protected.
• A non-static inner class has access to all member variables and methods of
the containing class.
• The only modifiers which can be applied to method local inner classes are
abstract and final
• Static nested classes are inner classes marked with the static modifier
Page 10Classification: Restricted
Class Declarations
• In a source file, there can be only one public class, and name of the file
should match that of the class
• Import and package declarations apply to all the classes in a source code
file
• Eg:
public class MyClass
{}
Page 11Classification: Restricted
Package Declarations
• If there is no package statement, the classes in the source file belong to the
default package, else they belong to the named package
• If there is a package statement, it should be the first line in the source file.
• Not more than one package statement is allowed in a source file
Eg:
package com.whiz.test;
class TestQuiz {}
Page 12Classification: Restricted
Import Statements
• Any import statements must be placed after the package statement, but
before the class declaration
• Import statements can be of 2 types – wildcard import and explicit class
import
eg:
import java.util.*;
// wildcard import - imports all classes in java.util package
import java.util.ArrayList;
// explicit import- imports ArrayList class in java.util package
Page 13Classification: Restricted
Determining Access to Class Members
Visibility public protected default private
From the same class Yes Yes Yes Yes
From any class in the same package Yes Yes Yes No
From any non-subclass class
outside the package
Yes No No No
From a subclass in the same
package
Yes Yes Yes No
From a subclass outside the same
package
Yes Yes No No
Page 14Classification: Restricted
Interface Declarations
• An interface is like a fully abstract class, it is implicitly public and abstract
• Variables declared in an interface are implicitly public, static and final
• Interface methods are implicitly public, cannot be static
• A class can implement any number of interfaces
• An interface can extend any number of interfaces
Eg:
interface MyInterface{
void f();
}
Page 15Classification: Restricted
Implementing an Interface
• A concrete class implementing an interface has to implement all the
methods declared in it
• The implemented method should not throw any new checked exception
not thrown by the interface method
• Interface methods are implicitly public, so cannot be overridden to be of
private, protected or default access
Eg:
interface MyInterface { void f(); }
class MyClass implements MyInterface{
public void f() {}
}
• The implemented method should maintain the exact signature and
return types of the interface method
Page 16Classification: Restricted
main() Method
• The main() method must be public and static
• The return type of the main() method should be void
• It should take only one argument, which should be an array of String
objects(name of the argument can be anything)
• The modifiers static and public can be in any order
Eg:
public static void main(String args[]) // Valid
{ }
static public void main(String abc[]) // Valid
{ }
Page 17Classification: Restricted
Extra points to remember
• A class or method cannot be both final and abstract
• A class with public modifier can be seen by classes in every package, else
it can be seen only by classes in the same package
• A class can be declared abstract even if it has no abstract methods, but it
has to be declared abstract if at least one of it methods is abstract
• The only modifier available for local variables is final
• Abstract methods cannot be private, final, synchronized, strictfp or
native
Page 18Classification: Restricted
Extra points to Remember…continued
• Final reference variables cannot refer to a different object, once they are
assigned to an object
• Abstract methods have no body, they end in a semicolon
• Protected members can be accessed by classes in the same package and
by subclasses anywhere
• A static method cannot access non-static variables
• A method local inner class cannot use the method’s local variables,
unless they are final
• A static nested class cannot access nonstatic members of the enclosing
class
• Static methods cannot be overridden
Page 19Classification: Restricted
Thank You

More Related Content

PDF
Access modifiers in java
PPTX
Access specifiers(modifiers) in java
PPTX
Visibility control in java
PDF
Non access modifiers
PPTX
10 access control
PPT
Access Protection
PPTX
Access modifiers in java
PPTX
C# Access modifiers
Access modifiers in java
Access specifiers(modifiers) in java
Visibility control in java
Non access modifiers
10 access control
Access Protection
Access modifiers in java
C# Access modifiers

What's hot (19)

PDF
Oops (inheritance&interface)
PDF
Chapter 03 enscapsulation
PPTX
[OOP - Lec 07] Access Specifiers
PPT
D2 Object Oriented Programming
DOC
116824015 java-j2 ee
PDF
Java modifiers
PPTX
Java Inner Class
PPTX
Inner class
PDF
Access specifiers (Public Private Protected) C++
DOCX
Master of Computer Application (MCA) – Semester 4 MC0078
PPTX
The smartpath information systems java
PDF
Lecture 10
PDF
Interface
PDF
Common Programming Errors by Beginners in Java
PPT
L5 classes, objects, nested and inner class
PPTX
Final presentation programming
PPT
Inner Classes
PDF
Inner Classes in Java
Oops (inheritance&interface)
Chapter 03 enscapsulation
[OOP - Lec 07] Access Specifiers
D2 Object Oriented Programming
116824015 java-j2 ee
Java modifiers
Java Inner Class
Inner class
Access specifiers (Public Private Protected) C++
Master of Computer Application (MCA) – Semester 4 MC0078
The smartpath information systems java
Lecture 10
Interface
Common Programming Errors by Beginners in Java
L5 classes, objects, nested and inner class
Final presentation programming
Inner Classes
Inner Classes in Java
Ad

Similar to Java Access Specifier (20)

PPTX
Session 11 - OOP's with Java - Packaging and Access Modifiers
PPSX
OOPs with Java - Packaging and Access Modifiers
PPTX
OOPs with Java - Packaging and Access Modifiers
PPTX
Introduction to Java Part-3
PPSX
Core Java for Selenium
PPT
encapsulation and abstraction
PPTX
Java OOPs
PPTX
Session 18 - Review Session and Attending Java Interviews
PPTX
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
PPTX
java interface and packages
PPSX
Intro to Object Oriented Programming with Java
PPT
Inheritance and Polymorphism
PPTX
Introduction to Java
PPTX
Introduction to Java
PPSX
Review Session and Attending Java Interviews
PPTX
Java Collection
PPTX
PPT Lecture-1.4.pptx
PPTX
Std 12 computer chapter 8 classes and object in java (part 2)
PPTX
Session 08 - OOP with Java - continued
PDF
Core Java Introduction | Basics
Session 11 - OOP's with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
Introduction to Java Part-3
Core Java for Selenium
encapsulation and abstraction
Java OOPs
Session 18 - Review Session and Attending Java Interviews
OCA Java SE 8 Exam Chapter 4 Methods Encapsulation
java interface and packages
Intro to Object Oriented Programming with Java
Inheritance and Polymorphism
Introduction to Java
Introduction to Java
Review Session and Attending Java Interviews
Java Collection
PPT Lecture-1.4.pptx
Std 12 computer chapter 8 classes and object in java (part 2)
Session 08 - OOP with Java - continued
Core Java Introduction | Basics
Ad

More from DeeptiJava (9)

PPT
Generating the Server Response: HTTP Status Codes
PPTX
Java Generics
PPTX
Java Exception Handling
PPTX
Java JDBC
PPTX
Java Thread
PPT
JSP Part 2
PPT
JSP Part 1
PPTX
Java I/O
PPT
Java Hibernate Basics
Generating the Server Response: HTTP Status Codes
Java Generics
Java Exception Handling
Java JDBC
Java Thread
JSP Part 2
JSP Part 1
Java I/O
Java Hibernate Basics

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PDF
KodekX | Application Modernization Development
PDF
cuic standard and advanced reporting.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Machine learning based COVID-19 study performance prediction
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Big Data Technologies - Introduction.pptx
Approach and Philosophy of On baking technology
KodekX | Application Modernization Development
cuic standard and advanced reporting.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
The AUB Centre for AI in Media Proposal.docx
Programs and apps: productivity, graphics, security and other tools
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Digital-Transformation-Roadmap-for-Companies.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Machine learning based COVID-19 study performance prediction
Building Integrated photovoltaic BIPV_UPV.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Unlocking AI with Model Context Protocol (MCP)
Big Data Technologies - Introduction.pptx

Java Access Specifier

  • 2. Page 1Classification: Restricted Agenda • Declarations and Access Control • Class Modifiers • Access Modifiers for Members • Non-Access Modifiers for Members • More Member Modifiers • Modifiers for Inner Classes • Class Declarations • Interface Declarations • Implementing an Interface • main() Method • Extra points to remember
  • 4. Page 3Classification: Restricted Objective • Declare classes, nested classes, methods, instance variables, static variables and automatic (method local) variables making appropriate use of all permitted modifiers (such as public, final, static, abstract, etc.). State the significance of each of these modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.
  • 5. Page 4Classification: Restricted Objective •Identify correctly constructed package declarations, import statements, class declarations (of all forms including inner classes) interface declarations, method declarations (including the main method that is used to start execution of a class), variable declarations, and identifiers. •Identify classes that correctly implement an interface where that interface is either java.lang.Runnable or a fully specified interface in the question
  • 6. Page 5Classification: Restricted Class Modifiers • Access Modifiers for Classes Top level classes and interfaces can be specified as public. If no access modifier is specified, the access level is default access • Other Modifiers for Classes abstract – The class cannot be instantiated final - The class cannot be extended ie; it cannot be subclassed
  • 7. Page 6Classification: Restricted Access Modifiers for Members • private - can only be accessed from inside the class. • protected - can only be accessed by classes in the same package or subclasses of this class • public - can be accessed by any other class. • If there is no access modifier specified- the members have default access level ie; they can be accessed only by other classes in the same package
  • 8. Page 7Classification: Restricted Non-Access Modifiers for Members • synchronized – only for methods. Can be accessed only by one thread at a time. • transient – only for variables. Cannot be serialized. • abstract – only for methods. Method is not implemented. Has to be implemented by the first concrete subclass • final – For methods, it means that the method cannot be inherited. For variables, it makes the variable a constant.
  • 9. Page 8Classification: Restricted More Member Modifiers • native – only for methods, means that the method is not written in Java, but in a native language. • strictfp – only for methods and classes. Forces floating points to adhere to IEE754 standard. • volatile – only for variables. The compiler will not perform optimization on the variables if this is specified. • static – for variables and methods. Static variables are shared by all instances of the class. Static methods and variables can be used without having any instances of that class at all.
  • 10. Page 9Classification: Restricted Modifiers for Inner Classes • Modifiers which can be applied to non-static inner classes are final, abstract, public and protected. • A non-static inner class has access to all member variables and methods of the containing class. • The only modifiers which can be applied to method local inner classes are abstract and final • Static nested classes are inner classes marked with the static modifier
  • 11. Page 10Classification: Restricted Class Declarations • In a source file, there can be only one public class, and name of the file should match that of the class • Import and package declarations apply to all the classes in a source code file • Eg: public class MyClass {}
  • 12. Page 11Classification: Restricted Package Declarations • If there is no package statement, the classes in the source file belong to the default package, else they belong to the named package • If there is a package statement, it should be the first line in the source file. • Not more than one package statement is allowed in a source file Eg: package com.whiz.test; class TestQuiz {}
  • 13. Page 12Classification: Restricted Import Statements • Any import statements must be placed after the package statement, but before the class declaration • Import statements can be of 2 types – wildcard import and explicit class import eg: import java.util.*; // wildcard import - imports all classes in java.util package import java.util.ArrayList; // explicit import- imports ArrayList class in java.util package
  • 14. Page 13Classification: Restricted Determining Access to Class Members Visibility public protected default private From the same class Yes Yes Yes Yes From any class in the same package Yes Yes Yes No From any non-subclass class outside the package Yes No No No From a subclass in the same package Yes Yes Yes No From a subclass outside the same package Yes Yes No No
  • 15. Page 14Classification: Restricted Interface Declarations • An interface is like a fully abstract class, it is implicitly public and abstract • Variables declared in an interface are implicitly public, static and final • Interface methods are implicitly public, cannot be static • A class can implement any number of interfaces • An interface can extend any number of interfaces Eg: interface MyInterface{ void f(); }
  • 16. Page 15Classification: Restricted Implementing an Interface • A concrete class implementing an interface has to implement all the methods declared in it • The implemented method should not throw any new checked exception not thrown by the interface method • Interface methods are implicitly public, so cannot be overridden to be of private, protected or default access Eg: interface MyInterface { void f(); } class MyClass implements MyInterface{ public void f() {} } • The implemented method should maintain the exact signature and return types of the interface method
  • 17. Page 16Classification: Restricted main() Method • The main() method must be public and static • The return type of the main() method should be void • It should take only one argument, which should be an array of String objects(name of the argument can be anything) • The modifiers static and public can be in any order Eg: public static void main(String args[]) // Valid { } static public void main(String abc[]) // Valid { }
  • 18. Page 17Classification: Restricted Extra points to remember • A class or method cannot be both final and abstract • A class with public modifier can be seen by classes in every package, else it can be seen only by classes in the same package • A class can be declared abstract even if it has no abstract methods, but it has to be declared abstract if at least one of it methods is abstract • The only modifier available for local variables is final • Abstract methods cannot be private, final, synchronized, strictfp or native
  • 19. Page 18Classification: Restricted Extra points to Remember…continued • Final reference variables cannot refer to a different object, once they are assigned to an object • Abstract methods have no body, they end in a semicolon • Protected members can be accessed by classes in the same package and by subclasses anywhere • A static method cannot access non-static variables • A method local inner class cannot use the method’s local variables, unless they are final • A static nested class cannot access nonstatic members of the enclosing class • Static methods cannot be overridden