You are on page 1of 21

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
Abstraction

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

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 s=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

ABSTRACTION

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

Abstraction

What is an Abstraction?
Hiding essential details and unhiding the non-essential details.

How can we implement Abstraction


Using Access specifiers.

What is an Access specifier?


It specifies the accessibility.

How many Access specifiers are there?


4 Access specifiers
1. private
Three are
2. public
keywords
3. protected
4. default
CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

1. private

Private members can't access outside the class.

Access within
the class

class Abc
{
private int x,y;
}

CS F213 OOP Summer Term 2015

class Bbc
{
Cant access

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

2. public

Public members can access anywhere in the Java program.

Access to all
the class

class Abc
{
public int x,y;
}

CS F213 OOP Summer Term 2015

class Bbc
{
Can access

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

3. protected

Protected members can be accessed when there is relation between classes


(when classes in different packages).
p1

p2

class Abc
{
protected int a;
}

class Xyz
{

If and only if Xyz


extends Abc

Protected members can be accessed between the classes, even if there is no


relation (classes with in the same package).
p1

Can be accessed

CS F213 OOP Summer Term 2015

class Abc{
protected int a;
}
class Xyz
{ -----}

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

4. default

Default members can be accessed with in the same package only.


p1

Abc

Can access
default members

Xyz

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

Summary- Access specifiers


1.
2.
3.
4.

Private within the class


Public anywhere in Java environment [even if it is non-child class]
Protected same package+ other package[only if there is a relation
between classes]
Default within the same package/folder.

Very Imp : Encapsulation and Abstraction goes hand in hand

CS F213 OOP Summer Term 2015

Prafulla Kalapatapu

BITS Pilani, Hyderabad Campus

You might also like