Session 3
Session 3
Scenario:
Sam
Accessing Class Members (Contd.)
Scenario (Contd.):
private
protected
public
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.
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>;
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.
Using Access Modifiers (Contd.)
The various modifiers permitted in Java are:
volatile strictfp
The static keyword :
Counter2(){
count++;//incrementing the value of static variable
System.out.println(count);
}
class Calculate{
static int cube(int x){
return x x x;
}
Microsoft Word
Document
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
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.
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
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
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