Object Slicing

Posted by CGN007 under C# category on | Points: 40 | Views : 7541
Object Slicing

In object-oriented programming, a childclass extends its superclass
by defining its own additional member variables. If a superclass instance is assigned
its value from a childclass instance, member variables defined in the childclass
can not be copied, since the superclass has no place to store them.

But the base class's member variables defined in the derived object are copied to
the base class Object. This is referred as Object Slicing.

Please see the following example

public class Base
public int baseclassMember;

public class Derived : Base
public int derivedclassMmber;

public static void Main(string[] args)
Base baseObj = new Base();
Derived derivedObj = new Derived();
baseObj = derivedObj; //Here derivedObj having the member variables 'baseclassMember' and 'derivedclassMmber'.But only 'baseclassMember' is copied to baseObj


Comments or Responses

Login to post response