21
BITS Pilani Hyderabad Campus BITS Pilani Ms. Prafulla Kalapatapu Computer Science & Information Systems Group BITS-Pilani Hyderabad Campus [email protected]

CS_IS_F213_L3_25May

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

    [email protected]

  • BITS Pilani Hyderabad Campus

    Object Oriented Programming Summer term Semester 2015

  • CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus

    Encapsulation

    Abstraction

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

  • CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus

    ABSTRACTION

  • CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus

    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

    2. public

    3. protected

    4. default

    Abstraction

    Three are

    keywords

  • CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus

    Private members can't access outside the class.

    1. private

    class Abc { private int x,y; }

    class Bbc { }

    Access within

    the class Cant access

  • CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus

    2. public

    Public members can access anywhere in the Java program.

    class Abc { public int x,y; }

    class Bbc { }

    Access to all

    the class Can access

  • CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus

    Protected members can be accessed when there is relation between classes (when classes in different packages).

    Protected members can be accessed between the classes, even if there is no relation (classes with in the same package).

    3. protected

    class Abc { protected int a; }

    class Xyz { }

    p1 p2

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

    p1

    If and only if Xyz

    extends Abc

    Can be accessed

  • CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus

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

    4. default

    p1

    Can access

    default members Abc

    Xyz

  • CS F213 OOP Summer Term 2015 Prafulla Kalapatapu BITS Pilani, Hyderabad Campus

    1. Private within the class

    2. Public anywhere in Java environment [even if it is non-child class]

    3. Protected same package+ other package[only if there is a relation between classes]

    4. Default within the same package/folder.

    Very Imp : Encapsulation and Abstraction goes hand in hand

    Summary- Access specifiers