Introduction to Java
Accessing Class Members
Scenario:
Might need to hide sensitive
information outside the
ClassicJumble class, such as
the jumbled-up words to be
displayed to the game user.
Sam
Ver 1.0 Slide 1 of 17
Introduction to Java
Accessing Class Members (Contd.)
Scenario (Contd.):
To cater to the preceding requirement and
improve the security of a class, Sam can use
access specifiers and modifiers in Java.
Ver 1.0 Slide 2 of 17
Introduction to Java
Using Objects
Objects are used to access the members of a class.
The following code snippet shows how to access the data members
of a class:
object_name.data_member_name
Ver 1.0 Slide 3 of 17
Introduction to Java
Using Access Specifiers
An access specifier controls the access of class members.
The various types of access specifiers in Java are:
private
protected
public
Ver 1.0 Slide 4 of 17
Introduction to Java
Using Access Specifiers (Contd.)
The private access specifier allows a class to hide its member
variables and member methods from other classes.
The private members of a class are not visible outside a class.
They are visible only to the methods of the same class.
A top level class cannot be declared private in Java.
The following code snippet shows how to declare a data member of
a class as private:
private <data type> <variable name>;
An inner class can be declared private.
The class members with the protected access specifier can be
accessed by all the classes within the package and by the
subclasses outside the package.
Ver 1.0 Slide 5 of 17
Introduction to Java
Using Access Specifiers (Contd.)
The following code snippet shows how to declare a data member of
a class as protected:
protected <data type> <variable name>;
The class members with the public access specifier can be
accessed by classes present within the package or outside the
package.
You can access a public class, data member, or method within the
class in which they are defined, from the classes defined in the
same package or outside the package.
The following code snippet shows how to declare a data member of
a class as public:
public <data type> <variable name>;
Ver 1.0 Slide 6 of 17
Introduction to Java
Using Access Modifiers
Access modifiers:
Determine or define how the data members and methods are used in
other classes and objects.
Determine how the data members are used and modified by other
classes.
Ver 1.0 Slide 7 of 17
Introduction to Java
Using Access Modifiers (Contd.)
The various modifiers permitted in Java are:
final static abstract
native synchronized transient
volatile strictfp
Ver 1.0 Slide 8 of 17
Introduction to Java
Just a minute
Which one of the following options is called automatically when a
class is instantiated?
Constructor
Variable
Abstract method
Package
Ver 1.0 Slide 9 of 17
Introduction to Java
Just a minute (Contd.)
Solution:
Constructor
Ver 1.0 Slide 10 of 17
Introduction to Java
Activity 1.1: Creating and Executing a Class
Problem Statement:
ProGame Corporation has recently introduced a new department
involved in the development of games. The organization has hired
junior programmers for this department. They have been assigned a
project to develop the Hangman Game application in Java. Create the
class structure for the Hangman game. In addition, write the code to
display a menu that provides a player with the options to play the
game, view the game instructions, or exit the game. Also, accept a
menu input from the user and display it. Help the programmer to
achieve the preceding requirements.
Ver 1.0 Slide 11 of 17
Introduction to Java
Activity 1.1: Creating and Executing a Class (Contd.)
Solution: To perform the activity, refer the steps given in the
embedded document.
Microsoft Word
Document
Ver 1.0 Slide 12 of 17
Introduction to Java
Summary
In this session, you learned that:
Java provides powerful features that make it a popular and widely used
programming language. Some of these features are:
Simple
Object-oriented
Platform independence
Portable
Distributed
Secure
Robust
Multithreaded
Ver 1.0 Slide 13 of 17
Introduction to Java
Summary (Contd.)
The various components of the Java architecture are:
Source file
Class file
JVM
API
A program in Java comprises the following building blocks:
Classes
Data types
Class members
Packages
A class defines the characteristics and behavior of an object.
Keywords are the reserved words with a special meaning for a
language, which express the language features.
Ver 1.0 Slide 14 of 17
Introduction to Java
Summary (Contd.)
There are eight primitive data types in Java, which are further grouped
into the following categories:
Integer type
Floating point type
Boolean type
Character type
A wrapper class acts like an object wrapper and encapsulates the
primitive data types within the class so it can be treated like an object.
In Java, a class can contain the following members:
Variables
Methods
Objects
Inner classes
Ver 1.0 Slide 15 of 17
Introduction to Java
Summary (Contd.)
A variable represents a name that refers to a memory location where
some value is stored.
A method is a set of statements that is intended to perform a specific
task.
Constructors are used to construct an object of a class and initialize it.
An object is an instance of a class and has a unique identity.
A package is a collection of classes.
Objects are used to access the members of a class.
An access specifier controls the access of class members. The various
types of access specifiers in Java are:
private
protected
public
Ver 1.0 Slide 16 of 17
Introduction to Java
Summary (Contd.)
Access modifiers determine or define how the data members and
methods are used in other classes and objects.
The various modifiers permitted in Java are:
final
static
abstract
native
synchronized
transient
volatile
strictfp
Ver 1.0 Slide 17 of 17