Program
public class downcasting {
public static void main(String[] args)
{
fordd f;
benzz b;
BMWw bm;
carr c,cc,ccc;
c=new fordd();
cc=new benzz();
ccc=new BMWw();
f=(fordd)c; // downcasting or narrowing or specialization
b=(benzz)cc;
bm=(BMWw)ccc;
f.car();
b.car();
bm.car();
}
}
class carr
{
void car()
{
System.out.println("This super class car method");
}
}
class fordd extends carr
{
void car()
{
System.out.println("This sub class Ford car method");
}
}
class benzz extends carr
{
void car()
{
System.out.println("This sub class Benz car method");
}
}
class BMWw extends carr
{
void car()
{
System.out.println("This sub class BMW car method");
}
}
public class downcasting {
public static void main(String[] args)
{
fordd f;
benzz b;
BMWw bm;
carr c,cc,ccc;
c=new fordd();
cc=new benzz();
ccc=new BMWw();
f=(fordd)c; // downcasting or narrowing or specialization
b=(benzz)cc;
bm=(BMWw)ccc;
f.car();
b.car();
bm.car();
}
}
class carr
{
void car()
{
System.out.println("This super class car method");
}
}
class fordd extends carr
{
void car()
{
System.out.println("This sub class Ford car method");
}
}
class benzz extends carr
{
void car()
{
System.out.println("This sub class Benz car method");
}
}
class BMWw extends carr
{
void car()
{
System.out.println("This sub class BMW car method");
}
}
Output
Comments
Post a Comment
Share this to your friends