Slicing

From Wikipedia, the free encyclopedia

For the mechanical process, see cutting.
Look up slicing in
Wiktionary, the free dictionary.

In object-oriented programming, a subclass often holds more information than its superclass. Thus, if we assign an instance of the subclass to a variable of type superclass, there is not enough space to store the extra information, and it is sliced off.

Note that this does not happen when you use pointers or references to objects. Thus, a language like Java does not have slicing. The language C++, however, does.

For example,

class A
{
  int age;
  String name;
};

class B : public A
{
  bool is_male;
};

B b;
A a;
a = b; // slicing occurs, is_male is not copied.
In other languages