You are on page 1of 16

Presentation

Polymorphism

Name : Abdul Qayoom


Name: Muhammad Ali
Name:Amar Hussain
Polymorphism

many forms
water forms
Defination

Polymorphism:-

Polymorphism is a greek word meaning "many forms",


and it occurs when we have many classes that are
related to each other by inheritance.
Types Of Polymorphism

There are two types of polymorphism:

1.Compile Time Polymorphism:

2. Run Time Polymorphism:


1.Compile Time Polymorphism:
Compile-time polymorphism is obtained through method overloading. The
term method overloading allows us to have more than one method with the
same name. Since this process is executed during compile time, that is why
it is known as Compile Time Polymorphism.
Method Overloding
1) Same name
2) Same class
3) Different Arguments
class test
{
void show ( int a ) // one argument

{
OUTPU
System.out.println ("1");
} T
void show (String b ) // one argument
{ 1
System.out.println ("2");
}
public static void main (String args[])
{
Test t =new Test();
t.show (10);
}
}
class test
{
void show ( int a , String b) // two arguments

System.out.println ("1"); OUTPU


}
void show (String a, int b ) // two arguments T
{
System.out.println ("2");
1
}
public static void main (String args[])
{
Test t =new Test();
t.show (10 , “abc”);

}
}
What is RunTime Polymorphism

Method overriding is an example of runtime polymorphism. In


method overriding, a subclass overrides a method with the same
signature as that of in its superclass. During compile time, the
check is made on the reference type.
• When a chlid class extands a parent class the all of
its variable and method is accessible in the child
class. But when the child class overrides one of the
method of the parent class, then during the runtime
the method of child class is called rather than the
parent class, this is decided in the run time and not in
the compile time.
EXAMPLE
class Animal {
void makesound() {
public class main {
System.out.println("animal makes a sound");
public static void main(String[] args) {
} Animal animal1 = new Dog();
} Animal animal2 = new Cat();
class dog extends Animal {
animal1.makesound();
void makesound() { animal2.makesound();
System.out.println("dog barks"); }
}
}
}
class cat extends Animal { OUTPUT
void makesound() { Dog barks
Cat meows
System.out.println("cat meows");
}
}
In this example, both the Dog and Cat classes override the make Sound
method inherited from the Animal class. When you create instances of Dog
and Cat and call the make Sound method on them, the appropriate
overridden method is invoked based on their actual runtime types.
Thank You

You might also like