PROGRAM-1 FOR RUN TIME POLYMORPHISM
class base
{
int x;
base()
{
x=100;
}
void display()
{
System.out.println("I AM INSIDE THE BASE CLASS");
System.out.println("The value of x is "+x);
}}
class derrived extends base
{
int y;
derrived()
{
y=500;
}
void display()
{System.out.println("I AM INSIDE THE DERRIVED CLASS");
System.out.println("The value of y is "+y);
}
}
class rpoly
{
public static void main(String args[])
{
base b=new base();
derrived d=new derrived();
base r;
r=b;
r.display();
//here i am refering the derrived class object by using base class reference
r=d;
r.display();
}
}
OUTPUT:-
//PROGRAM-2
class op
{
int n1,n2;
op(int a,int b)
{
n1=a;
n2=b;
}
void display()
{
System.out.println("I AM INSIDE THE OP CLASS");
System.out.println("The values of n1 and n2 are "+n1+n2);
}
int result()
{
System.out.println("THE RESULT IS NOTHING NOW I AM INSIDE THE OP CLASS");
return n1+n2;
}
}
class add extends op
{
add(int a,int b)
{
super(a,b);
}
void display()
{
System.out.println("I AM INSIDE THE DERRIVED ADD CLASS");
System.out.println("The values of n1 and n2 are"+n1+n2);
}
int result()
{
System.out.println("NOW I AM INSIDE THE ADD CLASS");
return n1+n2;
}
}
class sub extends op
{
sub(int a,int b)
{
super(a,b);
}
void display()
{
System.out.println("I AM INSIDE THE DERRIVED SUB CLASS");
System.out.println("The values of n1 and n2 are"+n1+n2);
}
int result()
{
System.out.println("NOW I AM INSIDE THE SUB CLASS");
return n1-n2;
}
}
class rpoly4
{
public static void main(String args[])
{
op o=new op(0,0);
add a=new add(10,20);
sub s=new sub(20,10);
op r;//i will use this reference to refer the various derrived class objects
r=o;
r.display();
r.result();
//here i am refering the derrived class add by using base class reference
r=a;
r.display();
int sum=r.result();
System.out.println("The result is addition "+sum);
//ref pointing to the derrived class sub
r=s;
r.display();
System.out.println("The result is subtraction"+r.result());
}
}
OUTPUT:-
class base
{
int x;
base()
{
x=100;
}
void display()
{
System.out.println("I AM INSIDE THE BASE CLASS");
System.out.println("The value of x is "+x);
}}
class derrived extends base
{
int y;
derrived()
{
y=500;
}
void display()
{System.out.println("I AM INSIDE THE DERRIVED CLASS");
System.out.println("The value of y is "+y);
}
}
class rpoly
{
public static void main(String args[])
{
base b=new base();
derrived d=new derrived();
base r;
r=b;
r.display();
//here i am refering the derrived class object by using base class reference
r=d;
r.display();
}
}
OUTPUT:-
E:\jit1>javac
rpoly.java
E:\jit1>java
rpoly
I AM INSIDE
THE BASE CLASS
The value of
x is 100
I AM INSIDE
THE DERRIVED CLASS
The value of
y is 500
class op
{
int n1,n2;
op(int a,int b)
{
n1=a;
n2=b;
}
void display()
{
System.out.println("I AM INSIDE THE OP CLASS");
System.out.println("The values of n1 and n2 are "+n1+n2);
}
int result()
{
System.out.println("THE RESULT IS NOTHING NOW I AM INSIDE THE OP CLASS");
return n1+n2;
}
}
class add extends op
{
add(int a,int b)
{
super(a,b);
}
void display()
{
System.out.println("I AM INSIDE THE DERRIVED ADD CLASS");
System.out.println("The values of n1 and n2 are"+n1+n2);
}
int result()
{
System.out.println("NOW I AM INSIDE THE ADD CLASS");
return n1+n2;
}
}
class sub extends op
{
sub(int a,int b)
{
super(a,b);
}
void display()
{
System.out.println("I AM INSIDE THE DERRIVED SUB CLASS");
System.out.println("The values of n1 and n2 are"+n1+n2);
}
int result()
{
System.out.println("NOW I AM INSIDE THE SUB CLASS");
return n1-n2;
}
}
class rpoly4
{
public static void main(String args[])
{
op o=new op(0,0);
add a=new add(10,20);
sub s=new sub(20,10);
op r;//i will use this reference to refer the various derrived class objects
r=o;
r.display();
r.result();
//here i am refering the derrived class add by using base class reference
r=a;
r.display();
int sum=r.result();
System.out.println("The result is addition "+sum);
//ref pointing to the derrived class sub
r=s;
r.display();
System.out.println("The result is subtraction"+r.result());
}
}
OUTPUT:-
E:\jit1>java
rpoly4
I AM INSIDE
THE OP CLASS
The values
of n1 and n2 are 00
THE RESULT
IS NOTHING NOW I AM INSIDE THE OP CLASS
I AM INSIDE
THE DERRIVED ADD CLASS
The values
of n1 and n2 are1020
NOW I AM
INSIDE THE ADD CLASS
The result
is addition 30
I AM INSIDE
THE DERRIVED SUB CLASS
The values
of n1 and n2 are2010
NOW I AM
INSIDE THE SUB CLASS
The result
is subtraction10
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి