Вы находитесь на странице: 1из 28

BITS Pilani

BITS Pilani
Hyderabad Campus

Ms. Prafulla Kalapatapu


Computer Science & Information Systems Group
BITS-Pilani Hyderabad Campus
prafulla@hyderabad.bits-pilani.ac.in

BITS Pilani
Hyderabad Campus

Object Oriented Programming


Summer term Semester 2015

Todays Agenda
Encapsulation

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

Encapsulation
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

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

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
{

User defined name

Variables/fields/properties
+
Methods/operations/behaviour

Class body

}
CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

Name Conventions

For Class Name :


Ex: Employee, Student
- Class name starts with a capital letter
- If class name contains multiple words each word should start with capital letter.
Ex: HelloWorld

For Variables :
- Variable names should be in small letters.

Ex: int total;

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();

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

Variables

Purpose is to store the data


1. Instance variables/object level/attributes

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)

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

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.

Similarities between instance and static variables


Both are used to store data

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

Variables [2]

Difference between instance and static variables.


Instance Variables
No static keyword

Static Variables
Declaration with keyword static

Memory allocated when object Memory is allocated when class


is created
is loaded into memory
For each object, separate copy For all objects, only one copy
of instance variable will be of static variable created
created
Memory is created in Heap

CS F213 OOP Summer Term 2015

Memory is created in Method


area

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

Method

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
}

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

Method [1]

Method Header or Method prototype: It contains


Return type
Method name
Method Parameters

Method Signature: It contains


Method name

Method Parameters

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

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.

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

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

BITS Pilani, Hyderabad Campus

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].

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

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

Suppose we have saved above program with ABC.java

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

Machine
Independent

JVM

Machine
code

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

Example for class:


Keyword

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

BITS Pilani, Hyderabad Campus

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.

How can we create an Object?


Using new keyword.

Syntax to create an Object


classname refname=new classname();
To hold an object

CS F213 OOP Summer Term 2015

Object creation (4 Step Process)

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

Example
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();

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

4 Step Object Creation


Process
Ex:
class Sample
{
int a=10;
String s;
boolean b;
public static void main(String []args)
{
Sample s1=new Sample();
}

Step 1

}
Object Creation Process

CS F213 OOP Summer Term 2015

Step 2
Step 3
Step 4

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

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

Any class null


type
variable
CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

Step 2
Assign instance variable with given values
Representation in memory :

10

CS F213 OOP Summer Term 2015

null

false

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

Step 3 & 4
3. Constructor will be called.
- Constructor is a special method.

4. Last step returns memory reference/address.

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

Constructor

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

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 It is a method i.e it calls


calls implicitly when object is explicitly
going to be created as a 3rd
step.
classname == methodname

It can be any name

It should not have return type It should have return type


Purpose is to intialize object Purpose is to perform some
state
task

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

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

Note: Any class it is implicitly


derived from an Object class

It calls the super


class constructor

}
CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

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

What is the responsibility of super();


super() calls the super classs constructor.
CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

super();

BITS Pilani, Hyderabad Campus

Вам также может понравиться