Lecture-1.2.2
Lecture-1.2.2
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
PROJECT BASED LEARNING IN JAVA WITH LAB
(22CSH-359/22ITH-359)
TOPIC OF PRESENTATION:
Polymorphism, Encapsulation and data privacy. (CO 2)
2
Polymorphism
Polymorphism in Java is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many
and "morphs" means forms. So polymorphism means many forms.
There are two types of polymorphism in Java: compile-time polymorphism and runtime
polymorphism. We can perform polymorphism in java by method overloading and method
overriding.
If you overload a static method in Java, it is the example of compile time polymorphism. Here,
we will focus on runtime polymorphism in java.
3
There are two types of polymorphism in Java: compile-time polymorphism and runtime
polymorphism. We can perform polymorphism in java by method overloading and
method overriding.
If you overload a static method in Java, it is the example of compile time polymorphism.
Here, we will focus on runtime polymorphism in java.
Upcasting
If the reference variable of Parent class refers to the object of Child class, it is known as
upcasting. For example:
class A{}
class B extends A{}
A a=new B();//upcasting
4
For upcasting, we can use the reference variable of class type or an interface type.
For Example:
interface I{}
class A{}
class B extends A implements I{}
5
Example of Java Runtime Polymorphism
class Bike{
void run(){System.out.println("running");}
}
class Splendor extends Bike{
void run(){System.out.println("running safely with 60km");}
6
Java Runtime Polymorphism with Data Member
Rule: Runtime polymorphism can't be achieved by data members.
7
Encapsulation
Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a
capsule which is mixed of several medicines.
• Protective Barrier to prevent data being directly used outside the class
• Hides the implementation level details.
8
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
9
Data Privacy using Encapsulation
• Encapsulated data is accessed using the “Accessor (getter)” and
“Mutator (setter)” methods.
10
QUIZ:
2. If same message is passed to objects of several different classes and all of those can
respond in a different way, what is this feature called?
a) Inheritance
b) Overloading
c) Polymorphism
d) Overriding
Summary:
Video Lectures :
https://youtu.be/jg4MpYr1TBc
Reference Links:
https://www.javatpoint.com/runtime-polymorphism-in-java
https://www.javatpoint.com/encapsulation
https://www.tutorialspoint.com/java/java_encapsulation.htm
https://www.geeksforgeeks.org/encapsulation-in-java/
THANK YOU