Polymorphism:

One single object which is used for many purpose that is called polymorphism.

Example : Smart watches – it is used as timing and also for calling , health porpose etc.

Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks.

Program: super class

class MoviE{
String Mname;
void playingmovie(){
System.out.println(“playingmovie”);
}
}

subclass 1:

class Smovie extends MoviE{
String Sname;
void playingmovie(){
System.out.println(“playing 2D movie”);
}
}

subclass 2:

class Dmovie extends MoviE{
String Dname;
void playingmovie(){
System.out.println(“playing 3D movie”);
}
}

main class:

class Screen{
public static void main(String args[]){
MoviE m=new MoviE();
Smovie s=new Smovie();
Dmovie d=new Dmovie();
s.playingmovie();
d.playingmovie();
}
}

Program:

sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Screen.javasobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Screenplaying 2D

movieplaying 3D movie


Leave a comment

Design a site like this with WordPress.com
Get started