
1)
Encapsulation:
The data members and the method are bound in a single unit and
wrapped by a class. This feature of object oriented programming is
called encapsulation. The data members’ visibility can be checked
from being accessed outside the class.
2)
Visibility control of variables and method:
. Variables and
methods of a class are accessible anywhere in the program. In some
situations the need of restricting the access to some variable or
methods may arise. This can be controlled by visibility modifiers or
“Access specifies “
3)
Visibility Modifiers / Access Modifiers:
These are the
keywords that precede the declaration of variables or methods. They
restrict the accessibility of the variables. Java provides fours
kinds of access : public, private, protected and friendly (the
default access).
Public: It
allows the variables (data members) and methods visible to all
classes outside the class in which it is defined.
Syntax
Public datatype variable name
Public <return type> methodname();
Example
Public int ac;
Public void sum(int a, int b)
{
int c = a + b;
System.out.println©;
}
Protected:
The protected variables and methods are accessible in all classes
and sub-classes of the same package as well as to sub-classes in
other packages.
Private:
The private members are accessible only within the class in they are
defined. They cannot be inherited to derived classes.
4)
Friendly Classes:
When the
declaration of the members is not preceded by any access modifier
(the default access) then these members can be accessed in all
classes within the same package.
5)
Scope of variable: Scope refers to the region where the
variable van be accessed depending on the part of the program where
they are declared. Depending on the scope, they are classified as
instance variable, class variables and local variables.
6)
Local variables: Variables declared and used within a method are
called local variables. They cannot be used outside the body of the
method.
For example,
void sum()
{
int a = 5,b=10,c;
c
= a + b;
System.out.println©;
}
Here scope of a,b,
and c is the method sum() so they are local variables
7)
Instance variables / Member variables: They are declared in the
class body but they are not contained in any constructor or method.
For example
class abc
{
int num1;
double num2;
abc()
{
num1 =
0;
num2 =
0;
}
}
Here num1, num2
are instance variables. They may take different values for each
object of the class as whenever an object is created, a new copy of
memory location(instance variable) is created
8)
Class variables :
These variables are associated with the class rather than its
objects,
The values that
they take are global to the class and do not belongs to any objects
they are created by precedind the variable declaration statement
with the keyword static.
For ex,
class abc
{
static in x , y;
static void sum()
{
statements;
}
};
9)
Interface:
Is is a set variables and methods like a class. The other classes
may implements an interface.
 |