Академический Документы
Профессиональный Документы
Культура Документы
BITS Pilani
Hyderabad Campus
BITS Pilani
Hyderabad Campus
Todays Agenda
Encapsulation
Prafulla Kalapatapu
ENCAPSULATION
Prafulla Kalapatapu
Encapsulation
What is an Encapsulation?
Combining data and code at one place as a module.
Prafulla Kalapatapu
Cont..
What is a class?
- Class is an user defined type, which contains variables and methods.
- It is a blue print/template for creating objects.
- It doesnt exist physically (i.e memory wont be allocated).
What is an Object?
Object is an instance of class
Syntax
Keyword
class classname
{
Variables/fields/properties
+
Methods/operations/behaviour
Class body
}
CS F213 OOP Summer Term 2015
Prafulla Kalapatapu
Name Conventions
For Variables :
- Variable names should be in small letters.
For Constants:
- Constant variable names should be in capital letters
Ex: final int PI=3.14;
For Methods:
- Method name starts with a small letter
- From second word onwards,each words first letter will be in capital letter
Ex: taxCalculation();
Prafulla Kalapatapu
Variables
Variables
2. Static variables/class level.
1. Instance Variables:
If we declare any variable in class, then we can call those variables
as instance variables.
- When memory allocates for instance variable?
When we create an instance/object, at that time, memory will
allocate for instance variable.
- Can we use multiple instances for a class?
Yes (any no.of)
Prafulla Kalapatapu
Variables [1]
2. Static Variables:
If we declare any variable with a keyword static, in a class, we can call
those variables as static variables.
- When memory allocates for static variable?
When the class is loaded into the memory, at that time, memory
will be allocated to static variable.
Prafulla Kalapatapu
Variables [2]
Static Variables
Declaration with keyword static
Prafulla Kalapatapu
Method
What is a method?
If we write a function in a class,we can call it as method.
Syntax:
returntype methodname()
{
//method body
}
Prafulla Kalapatapu
Method [1]
Method Parameters
Prafulla Kalapatapu
Method [2]
1. Instance Methods.
Methods
2. Static Methods.
1. Instance Method:
Defining /writing any method in a class, we can call it as instance
method.
- How we can call instance method?
Using object name.
Syntax: objectname.method();
v1.calc();
- To call instance method, we should create an object.
Prafulla Kalapatapu
Method [3]
2. Static Method:
Defining/writing a method with the keyword static, we can call it as
static method.
- How we can call static method?
Using classname
Syntax: classname.methodname();
Ex: Sample.m1();
- Limitation of static method:
static void m1()
{
// cant access instance variables/methods
}
- what static method can access?
static variables/methods
local variables
CS F213 OOP Summer Term 2015
Prafulla Kalapatapu
Method [4]
Reason for not accessing instance variables in static methods:
First when class is loaded, for all static variables/methods/blocks, memory will
be allocated in the method area.
Later, objects/instance will be created in the heap area.
So, later part [objects/heap area] cant be accessed in prior part [method
area].
But prior part [method area] can be accessed in later part [objects/heap area].
Prafulla Kalapatapu
Example:
Keyword
Class
body
class ABC
User defined classname
Pre-defined classname
{
public static void main(String []a)
{
System.out.println(hello);
}
Class name
Method
Method Prototype
}
Variable in
System class
Prafulla Kalapatapu
ABC.java
Java
Compiler
Byte code
ABC.class
Machine
Independent
JVM
Machine
code
Machine
Dependent
Prafulla Kalapatapu
Class
body
class Employee
User defined classname
{
int eid;
Instance variables
String ename;
double esal;
Static variable
Static int empcount;
void displayEmpDetails()
{
System.out.println(eid+ +ename);
Instance method
}
static void dispEmpCount()
{
System.out.println(empcount); //eid,ename,esal cant be accessed
Static method
}
}
CS F213 OOP Summer Term 2015
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();