Program Statement: Write a JAVA program to demonstrate runtime polymorphism.
Tested
on:
Software: Windows 8, Jdk 1.6, Netbeans IDE
Hardware: Intel Core i3, 4 GB RAM, 500 GB HDD
Software: Windows 8, Jdk 1.6, Netbeans IDE
Hardware: Intel Core i3, 4 GB RAM, 500 GB HDD
Source
Code
@Author: Praveen
Kanwar
abstract class Shape
{
abstract void area();
}
class Rectangle extends Shape
{
@Override
void area()
{
int l=10,b=20;
System.out.println("The area of rectangle is:"+l*b);
}
}
class Square extends Shape
{
void area()
{
int l=10;
System.out.println("The area of square is:"+l*l);
}
}
public class RuntimePolymorphism
{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Shape s;
Rectangle r=new Rectangle();
Square s1=new Square();
s=r;
s.area();
s=s1;
s.area();
}
}
abstract class Shape
{
abstract void area();
}
class Rectangle extends Shape
{
@Override
void area()
{
int l=10,b=20;
System.out.println("The area of rectangle is:"+l*b);
}
}
class Square extends Shape
{
void area()
{
int l=10;
System.out.println("The area of square is:"+l*l);
}
}
public class RuntimePolymorphism
{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Shape s;
Rectangle r=new Rectangle();
Square s1=new Square();
s=r;
s.area();
s=s1;
s.area();
}
}
Output
The area of rectangle is:200
The area of square is:100
The area of square is:100
Post a Comment