0% found this document useful (0 votes)
12 views4 pages

Gr. X File 9. Encapsulation-1

The document explains encapsulation in object-oriented programming, detailing access specifiers such as private, default, protected, and public, along with their accessibility levels. It also discusses the static keyword, differentiating between instance and class methods, and outlines variable scopes, including global and local variables. Additionally, it introduces the concept of packages and library classes in Java.

Uploaded by

Smite
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Gr. X File 9. Encapsulation-1

The document explains encapsulation in object-oriented programming, detailing access specifiers such as private, default, protected, and public, along with their accessibility levels. It also discusses the static keyword, differentiating between instance and class methods, and outlines variable scopes, including global and local variables. Additionally, it introduces the concept of packages and library classes in Java.

Uploaded by

Smite
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Encapsulation

Access Specifiers(Modifiers) / Visibility Modifiers: (Members of the class)


1. private –
private members are accessible within the class only and nowhere outside. They
are declared with the keyword ‘private’. They have least accessibility.
2. default/package/friendly –
default members are accessible within the class and all the classes of the same
package, but nowhere outside the package. No keyword is used for its
declaration.
3. protected –
protected members are accessible within the class, all the classes of the same
package as well as subclass outside the package. They are declared with the
keyword ‘protected’.
4. public –
public members are accessible within the class, package and outside, i.e.
everywhere. They are declared with the keyword ‘public’. They have most
accessibility.

Example to understand the visibility of members of the class :


P1 P2
class C1 class C3 extends C1
{ { }
private int a;
int b; class C4
protected int c; { }
public int d;
void fn1()
{ }
void fn2()
{ }
} // C1 ends
class C2
{ }
Description of the example –
Package P1 has 2 classes – C1 and C2.
Package P2 has 2 classes – C3 (a subclass of C1) and C4
Variables defined in C1 – a, b, c, d and following table shows its accessibility :
Variable C1 C2 C3 C4
a    
b    
C    
d    

static keyword
static keyword will make the member belong to the class and not an instance.
Only 1 copy exist for the entire class when members are declared as ‘static’.
Thus, data members or member functions not created for the instance but
created for the class are declared with the keyword static. Only 1 sharable copy
exist for the entire class for these types of members.
E.g.
class abc
{
int a,b; //instance members – so multiple copies can exist(depending on
number of instances/objects)
static int c; //class member – so single copy exists for the entire class
void function1()
{ ……….
……….
}
static void function2()
{ ……….
……….
}
} // class ends
Question : Differentiate between instance methods and class methods.
Ans.
instance methods class methods
1. Declared without any special 1. Declared with the keyword ‘static’.
keyword
2. method can be called by 2. method can be called by the class name.
object/instance
3. E.g. n = sc.nextInt(); 3. E.g. d = Math.sqrt(100);

Scope of variables (is always the set of braces for which they are declared)
Types of variables :
There are mainly two types of variables :
1. Global Variables 2. Local variables
(declared at a class level) (declared at a function level)

Object/ static/class local argument/parameter


instance variables variables variables
variables

Example :
class abc
{
int a, b; //instance variables
static int c; //class variable
void fn1(int x, int y)//x,y – parameter/argument variables
{
int z; //local variable
z=x + y;
System.out.println(z);
}
……………
}//class ends
Package:
Package is a collection of related or similar classes. To use the readymade
package in our class, keyword ‘import’ is used.
Examples of java API packages: lang, util, io etc.

Library Classes:
Classes present in Java Library inside a package are called Library classes.
Examples of Library classes: Math, String, Scanner etc.

You might also like