Upload
saimanobhiram
View
214
Download
1
Embed Size (px)
DESCRIPTION
java
Citation preview
BITS Pilani Hyderabad Campus
BITS Pilani Ms. Prafulla Kalapatapu
Computer Science & Information Systems Group
BITS-Pilani Hyderabad Campus
BITS Pilani Hyderabad Campus
Object Oriented Programming Summer term Semester 2015
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
Encapsulation
Todays Agenda
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
ENCAPSULATION
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
What is an Encapsulation?
Combining data and code at one place as a module.
How can we manage the data?
Using variables/fields/attributes
How can we manage the code?
Using methods/ functions
How can we implement Encapsulation in Java?
Using classes and objects
Encapsulation
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
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
class classname
{
}
Cont..
User defined name Keyword
Variables/fields/properties
+
Methods/operations/behaviour
Class body
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
For Class Name :
- Class name starts with a capital letter
- If class name contains multiple words each word should start with capital letter.
For Variables :
- Variable names should be in small letters.
For Constants:
- Constant variable names should be in capital letters
For Methods:
- Method name starts with a small letter
- From second word onwards,each words first letter will be in capital letter
Name Conventions
Ex: Employee, Student
Ex: HelloWorld
Ex: final int PI=3.14;
Ex: taxCalculation();
Ex: int total;
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
Purpose is to store the data
Variables
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)
Variables
1. Instance variables/object level/attributes
2. Static variables/class level.
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
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.
Similarities between instance and static variables
Both are used to store data
Variables [1]
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
Difference between instance and static variables.
Instance Variables Static Variables
No static keyword Declaration with keyword static
Memory allocated when object
is created
Memory is allocated when class
is loaded into memory
For each object, separate copy
of instance variable will be
created
For all objects, only one copy
of static variable created
Memory is created in Heap Memory is created in Method
area
Variables [2]
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
What is a method?
If we write a function in a class,we can call it as method.
What is the purpose of method?
Purpose is to perform some task.
Syntax:
returntype methodname()
{
//method body
}
Method
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
Method Header or Method prototype: It contains
Method Signature: It contains
Method [1]
Return type
Method name
Method Parameters
Method name
Method Parameters
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
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.
Method [2]
1. Instance Methods.
2. Static Methods.
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
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
Method [3]
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
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].
Method [4]
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
class ABC
{
public static void main(String []a)
{
System.out.println(hello);
}
}
Suppose we have saved above program with ABC.java
Example:
User defined classname
Keyword
Class
body
Class name
Pre-defined classname
Method Prototype
Variable in
System class
Method
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
Compilation & Execution process of JAVA
ABC.java Java
Compiler Byte code ABC.class
JVM Machine
code
Machine
Independent
Machine
Dependent
What is the responsibilitty of JVM?
It takes .class file and converts each byte code instruction into the
machine code with respective to individual OS
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
class Employee
{
int eid;
String ename;
double esal;
Static int empcount;
void displayEmpDetails()
{
System.out.println(eid+ +ename);
}
static void dispEmpCount()
{
System.out.println(empcount); //eid,ename,esal cant be accessed
}
}
Example for class:
User defined classname
Keyword
Class
body
Instance method
Static method
Instance variables
Static variable
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
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.
How can we create an Object?
Using new keyword.
Syntax to create an Object
classname refname=new classname();
Object
To hold an object Object creation (4 Step Process)
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
Employee e1 = new Employee();
Employee is a class name.
e1 is a reference variable of type Employee, to hold employee object (RHS).
In RHS, object will create (4 step process).
Alternate way to represent the same
Employee e1;
e1 = new Employee();
Example
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
Ex:
class Sample
{
int a=10;
String s;
boolean b;
public static void main(String []args)
{
}
}
4 Step Object Creation Process
Sample s1=new Sample();
Object Creation Process Step 3
Step 2
Step 4
Step 1
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
In RHS, It will create memory for all instance variables and initializes with
default values.
Representation of object in Memory :
Step 1
0
false null
a
s b
Data
Type
Default
Value
int 0
float 0.0
double 0.0
string Null
char \u0000
boolean false
Any class
type
variable
null
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
Assign instance variable with given values
Representation in memory :
Step 2
10
false null
a
s b
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
3. Constructor will be called.
- Constructor is a special method.
4. Last step returns memory reference/address.
Step 3 & 4
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
What is a constructor?
It is a special method.
What is the purpose of the constructor?
To initialize objects state. (Assigning values to instance variables).
Rules to write a constructor.
- classname == methodname
- It should not have return type
Constructor
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
Constructor Vs Method
Constructor Method
It is a special method i.e it
calls implicitly when object is
going to be created as a 3rd
step.
It is a method i.e it calls
explicitly
classname == methodname It can be any name
It should not have return type It should have return type
Purpose is to intialize object
state
Purpose is to perform some
task
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
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
{
}
Case (i)
Abc()
{
super(); }
Written by the
java compiler
It calls the super
class constructor
Note: Any class it is implicitly
derived from an Object class
CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus
If we write a constructor in the class
Ex:
class Abc
{
int a;
Abc()
{
a=20;
}
}
After compiling above class, your java compiler will add one statement as first line in the constructor
What is the responsibility of super();
super() calls the super classs constructor.
Case (ii)
super(); Written by java compiler after compiling
super();
NOTE: Default constructor is
written by java compiler, only
when there is no constructor in
the class