From this program you can get idea of object slicing
Code:
class parent
{
public int p,q;
}
class child extends parent
{
public int t;
}
class main
{
public static void main(String[] s)
{
parent x = new parent();
drived y = new child();
x.p=10;
x.q=20;
System.out.println("this is class parent p=10,q=20");
System.out.println("p =" +x.p);
System.out.println("j =" +x.q);
System.out.println("this is class child p=100,q=200,t=300");
y.=100;
y.j=200;
y.k=300;
System.out.println("p =" +y.p);
System.out.println("q =" +y.q);
System.out.println("t =" +y.t);
x=y;
System.out.println("after the assignment");
System.out.println("p =" +x.p);
System.out.println("q =" +x.q);
//System.out.println("t =" +x.t);
System.out.println("now you know what is object slicing");
}
}
Bookmarks