[go: up one dir, main page]

0% found this document useful (0 votes)
22 views27 pages

Polymorphism Presentation 09

Polymorphism is a programming concept that allows objects to behave differently based on their context, enabling code flexibility and efficiency. It can be categorized into compile-time polymorphism (method overloading) and run-time polymorphism (method overriding). Real-life examples include a universal remote control that operates various devices, demonstrating the principle of using the same interface for different underlying forms.

Uploaded by

adinaabid99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views27 pages

Polymorphism Presentation 09

Polymorphism is a programming concept that allows objects to behave differently based on their context, enabling code flexibility and efficiency. It can be categorized into compile-time polymorphism (method overloading) and run-time polymorphism (method overriding). Real-life examples include a universal remote control that operates various devices, demonstrating the principle of using the same interface for different underlying forms.

Uploaded by

adinaabid99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

polymorphism

UZAIR RAZA DSAI-242102003


ABDUR_REHAMAN DSAI-242102009
MAAZ HAFEEZ DSAI-242102016
Definition
Polymorphism is combination of two Greek words Poly and morphism
whose meaning is “same object having different behavior ”.

POL MORPHIS Frien Custome


Y M d r

MAN FOR PERSO


Y M N

Teacher Student
MANYFORM One person different
S behavior
Benefits:
Polymorphism allow one function or method to work in different ways
depending on context making code easier. In short, we can say same actions
in different ways.
Real Life Example:
 Remote control

One remote can work on different thing’s(objects) in different ways. We can


control TV, AC, Fans with one single universal designed remote.
Advantages:
 Less Code
 More Flexible
 Easier to Update
 Simpler
Program:
class Animal{
public void speak() {
System.out.print( “some animals sound” );
}
}
class Dog extends Animal{
@override
public void speak() {
System.out.println(“Woof!”);
}
}
Program:
class Cat extends Animal{
@override
public void speak(){
System.out.println(“Meow!”);
}
}
public class Main{
public static void animalSound(Animal animal){
animal.speak();
}
Program:
public static void main(String[] args){
Animal dog = new Dog();
Animal cat = new Cat();
animalSound(dog);
animalSound(cat);
}
}

Output:-
Woof!
Meow!
thak gia hon juice pila do
yaar
Types of Polymorphism
There are two types of polymorphism.
1. Compile Time Polymorphism.
2. Run-Time polymorphism.
Compile Time Polymorphism
A polymorphism which exists at the time of compilation is called
compile time or static polymorphism .
 Advantages:-

1. Type safety
2. Performance()
EXAMPLE:-
Method overloading:-
Whenever a class contain more than one method with same
name and different types of parameters is called overloading
Syntax
Void,
Array
return type method name(){….}
parameter
s
return type method name(){….}

Same
name
Code Example:
Class A{
void add(){
int a = 10 , b = 20 , c ;
c=a+b;
System.out.print( c ) ;
}
void add ( int x , double y ){
int c ;
c=x+y;
System.out.println( c );
}
Code Example:
void add ( int x , double y ){
double c ;
c=x+y;
System.out.println( c );
}
public static void main(String[] args){
A r=new A();
r.add();
r.add( 100 , 200 );
r.add( 10 , 35.40 );
}
}
Run-Time Polymorphism
Run-Time polymorphism happens when the program decides which
method to use while it is running. If a parent class reference is used to
call a method, the actual method that runs depends on which object
(either parent or child) the reference is pointing to at that moment.
This decision is made to runtime.
Example:
Method Overriding
Method overriding in Java is a feature that allows a subclass to
provide a specific implementation of a method that is already defined
in its superclass. It is used to modify or extend the functionality of an
inherited method.
Syntax:
public class MainClass {
public static void main(String[] args) {
superclass reference = new Subclass();
reference.methodName(parameters);
}}

You might also like