Sunteți pe pagina 1din 8

CHANDIGARH UNIVERSITY

University Institute of Engineering


Department of Computer Science &Engineering

Encapsulation Data privacy and Polymorphism

Prepared By- Vijay Kumar


Assistant Professor
CSE Department
Encapsulation
One of the fundamental OOP Concept
Protective Barrier to prevent data being directly used
outside the class
Hides the implementation level details
Example In Facebook people hides their age

Actual Detail is
hidden inside
Casule

UIE, CSE Deptt.


2
Data Privacy using
Encapsulation
Fields in a class are made private to prevent it to be
accessed by code outside the class.

Private fields can be accessed only by using the public


methods in the class

It leads to Data Hiding or Privacy

UIE, CSE Deptt.


3
Data Privacy using
Encapsulation
Encapsulated data is accessed using the Accessor
(getter) and
Mutator (setter) methods.

Accessors Methods to retrieve the hidden data.

Mutators Methods to change hidden data.

UIE, CSE Deptt.


4
Polymorphism
Capability of a method to do different things based on
the object used for Accessing the method.

Two Types-

Compile time Polymorphism(Early Binding)

Runtime Polymorphism(Late Binding)

UIE, CSE Deptt.


5
Compile time Polymorphism
Method Call is resolved by compiler

In java Compile time is implemented using Method


Overloading

Method overloading-One method can have different


signature and return type

UIE, CSE Deptt.


6
Run Time Polymorphism-
JVM determines the method call at the runtime and not
at the compile time.

The method being called is based on the object which on


which the method is triggered.

Method overriding is an example of runtime


polymorphism

UIE, CSE Deptt.


7
Questions to be Discussed
Can overloaded methods be override too?

Is it possible to override the main method?

How do you prevent a method from being overridden

UIE, CSE Deptt.


8

S-ar putea să vă placă și