Slicing
Encyclopedia
In object-oriented programming
, a subclass typically extends its superclass by defining additional member variables. If a superclass instance is assigned its value from a subclass instance, member variables defined in the subclass cannot be copied, since the superclass has no place to store them. This is a natural and unavoidable consequence of assignment by value from subclass objects. The term object slicing is sometimes used to refer to this aspect of assignment by value to a superclass instance.
Object slicing is also used to refer to a more subtle, problematic, case in which an object assignment by value appears to be to a superclass instance but is actually to a subclass instance. From the perspective of object memory layout, the member variables of the source instance can be thought of as having been "sliced off", leaving the corresponding member variables in the destination instance unchanged. It is this partial assignment (arguably a more apt term) that often surprises programmers and leads to unintended consequences.
Unexpected object slicing can happen in languages such as C++
in which assignment by value is not polymorphic
. It is not possible in Java
, which allows object assignment only by reference, or the D programming language
, which allows object inheritance only through reference types.
For example, in C++:
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
, a subclass typically extends its superclass by defining additional member variables. If a superclass instance is assigned its value from a subclass instance, member variables defined in the subclass cannot be copied, since the superclass has no place to store them. This is a natural and unavoidable consequence of assignment by value from subclass objects. The term object slicing is sometimes used to refer to this aspect of assignment by value to a superclass instance.
Object slicing is also used to refer to a more subtle, problematic, case in which an object assignment by value appears to be to a superclass instance but is actually to a subclass instance. From the perspective of object memory layout, the member variables of the source instance can be thought of as having been "sliced off", leaving the corresponding member variables in the destination instance unchanged. It is this partial assignment (arguably a more apt term) that often surprises programmers and leads to unintended consequences.
Unexpected object slicing can happen in languages such as C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
in which assignment by value is not polymorphic
Polymorphism in object-oriented programming
Subtype polymorphism, almost universally called just polymorphism in the context of object-oriented programming, is the ability to create a variable, a function, or an object that has more than one form. The word derives from the Greek "πολυμορφισμός" meaning "having multiple forms"...
. It is not possible in Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
, which allows object assignment only by reference, or the D programming language
D (programming language)
The D programming language is an object-oriented, imperative, multi-paradigm, system programming language created by Walter Bright of Digital Mars. It originated as a re-engineering of C++, but even though it is mainly influenced by that language, it is not a variant of C++...
, which allows object inheritance only through reference types.
For example, in C++: