Professional Documents
Culture Documents
BITS Pilani
Hyderabad Campus
BITS Pilani
Hyderabad Campus
Todays Agenda
Encapsulation
Abstraction
Prafulla Kalapatapu
ENCAPSULATION
Prafulla Kalapatapu
Object
What is an Object?
- Object is an instance of a class
- Object exist physically.[heap memory]
What is Instantiation?
Process of creation of an object.
Prafulla Kalapatapu
Example
Employee e1 = new Employee();
Prafulla Kalapatapu
Step 1
}
Object Creation Process
Step 2
Step 3
Step 4
Prafulla Kalapatapu
Step 1
In RHS, It will create memory for all instance variables and initializes with
default values.
Representation of object in Memory :
Data
Type
Default
Value
int
float
0.0
double
0.0
string
Null
char
\u0000
boolean
false
null
false
Prafulla Kalapatapu
Step 2
Assign instance variable with given values
Representation in memory :
10
null
false
Prafulla Kalapatapu
Step 3 & 4
3. Constructor will be called.
- Constructor is a special method.
Prafulla Kalapatapu
Constructor
What is a constructor?
It is a special method.
Prafulla Kalapatapu
Constructor Vs Method
Constructor
Method
Prafulla Kalapatapu
Case (i)
If we dont write a constructor, java compiler will write default constructor
Ex:
class Abc
{
}
In the above class Abc, we didnt write any constructor, when you compile this
class, your java compiler will write default constructor as shown below.
Ex:
class Abc
{
Abc()
{
super();
}
Written by the
java compiler
}
CS F213 OOP Summer Term 2015
Prafulla Kalapatapu
Case (ii)
If we write a constructor in the class
Ex:
class Abc
NOTE: Default constructor is
{
written by java compiler, only
int a;
when there is no constructor in
the class
Abc()
Written by java compiler
{
super();
after compiling
a=20;
}
}
After compiling above class, your java compiler will add one statement as
first line in the constructor
Prafulla Kalapatapu
super();
ABSTRACTION
Prafulla Kalapatapu
Abstraction
What is an Abstraction?
Hiding essential details and unhiding the non-essential details.
Prafulla Kalapatapu
1. private
Access within
the class
class Abc
{
private int x,y;
}
class Bbc
{
Cant access
Prafulla Kalapatapu
2. public
Access to all
the class
class Abc
{
public int x,y;
}
class Bbc
{
Can access
Prafulla Kalapatapu
3. protected
p2
class Abc
{
protected int a;
}
class Xyz
{
Can be accessed
class Abc{
protected int a;
}
class Xyz
{ -----}
Prafulla Kalapatapu
4. default
Abc
Can access
default members
Xyz
Prafulla Kalapatapu
Prafulla Kalapatapu