Polymorphism

1.Definition:


  • An object showing different behavior at different stages of the life cycle is called polymorphism.
  • Poly means many, morphism means forms.
  • In polymorphism, we have two types
    • Run-time polymorphism.
    • Compile-time polymorphism.

2.Run-time polymorphism:


Example 1:


class Network
{
void plan()
{
System.out.println("Plan for Jio,Airtel,Jio");
}
}
class Jio extends Network
{
void plan()
{
System.out.println("Plan for Jio Rs. 49");
}
}
class Airtel extends Network
{
void plan()
{
System.out.println("Plan for Airtel Rs. 149");
}
}
class Vodafone extends Network
{
void plan()
{
System.out.println("Plan for Vodafone Rs. 111");
}
}
class Stimulator
{
static void sim(Network a1)
{
a1.plan();
}
}
class Sim
{
public static void main(String args[])
{
System.out.println("*** Main Start***");
Jio j=new Jio();
Airtel a=new Airtel();
Vodafone v=new Vodafone();
Stimulator.sim(j);
Stimulator.sim(a);
Stimulator.sim(v);
System.out.println("***Main end***");
}

output:    
*** Main Start***
Plan for Jio Rs. 49
Plan for Airtel Rs. 149
Plan for Vodafone Rs. 111
***Main end***

Example 2:





class Driving
{
void license()
{
System.out.println("license price..");
}
}
class twowheeler extends Driving
{
void license()
{
System.out.println("Rs 1000");
}
}
class fourwheeler extends Driving
{
void license()
{
System.out.println("Rs. 2000");
}
}
class sixwheeler extends Driving
{
void license()
{
System.out.println("Rs. 3000");
}
}
class Stimulator
{
static void lic_sim( Driving d1)
{
d1.license();

}
}

class Car
{
public static void main(String args[])
{
twowheeler w=new twowheeler();
fourwheeler w1=new fourwheeler();
sixwheeler w2=new sixwheeler();
Driving d=new Driving();
Stimulator.lic_sim(d);
Stimulator.lic_sim(w);
Stimulator.lic_sim(w1);
Stimulator.lic_sim(w2);

}

}

Example 3:



class PaytmS
{
void recharge()
{
System.out.println("Recharge mobile |  DTH | Metrocard");
}
}
class Mobile extends PaytmS
{
void recharge()
{
System.out.println(" Jio : Rs.49");
}
}
class Dth extends PaytmS
{
void recharge()
{
System.out.println(" Dth: Rs.200");
}
}

class Metrocard extends PaytmS
{
void recharge()
{
System.out.println(" Metrocard : Rs. 2000");
}
}
class Stimulator
{
static void sim(PaytmS p)
{
p.recharge();
}
}
class Paytm
{
public static void main(String args[])
{
Mobile m=new Mobile();
Dth d=new Dth();
Metrocard m1=new Metrocard();
Stimulator.sim(m);
Stimulator.sim(d);
Stimulator.sim(m1);
}

}

Example 4:

class Drinks
{
void drink()
{
System.out.println("Lets Party");
}
}
class Hotdrinks  extends Drinks
{
void drink()
{
System.out.println("Give me Hot drink");
}
}
class Softdrink extends Drinks
{
void drink()
{
System.out.println("Give me Soft drink");
}
}
class Energydrink extends Drinks
{
void drink()
{
System.out.println("Give me energy drink");
}
}
class Stimulator
{
static void sim(Drinks d)
{
d.drink();
}
}

class Party
{
public static void main(String args[])
{
Hotdrinks h=new Hotdrinks();
Softdrink s=new Softdrink();
Energydrink e=new Energydrink();
Stimulator.sim(h);
Stimulator.sim(s);
Stimulator.sim(e);

}

}

Example 5:

class Browser
{
void  access()
{
System.out.println("Health");
}
}
class Fever  extends Browser
{
void access()
{
System.out.println("Typhoid");
}
}
class Cancer extends Browser
{
void access()
{
System.out.println("Cig");
}
}
class Migrane extends Browser
{
void acess()
{
System.out.println("Brain");
}
}
class Stimulator 
{
static void sim(Browser b)
{
b.access();
}
}

class Health
{
public static void main( String args[])
{
Fever f=new Fever();
Cancer c=new Cancer();
Migrane m=new Migrane();
Stimulator.sim(f);
Stimulator.sim(c);
Stimulator.sim(m);

}}


Comments

Popular posts from this blog

How to set image in carousel using flask?

Invalid syntax , perhaps you forgot a comma? Error in Python

How to run PL/SQL Code With Command Line?