An object is polymorphic when its specific type may vary.
The types a specific value may take, is called class.

It is trivial to copy an object if its type is known:

int x;
int y = x;
Here x is not polymorphic, so y is declared of same type (int) as x.
But if the specific type of x were unknown, then y could not be declared of any specific type.

The task: let a polymorphic object contain an instance of some specific type S derived from a type T.
The type T is known.
The type S is possibly unknown until run time.
The objective is to create an exact copy of such polymorphic object (not to create a reference, nor a pointer to).
Let further the type T have a method overridden by S.
This method is to be called on the copy to demonstrate that the specific type of the copy is indeed S.

 
abstract type Jewel end
 
mutable struct RoseQuartz <: Jewel
    carats::Float64
    quality::String
end
 
mutable struct Sapphire <: Jewel
    color::String
    carats::Float64
    quality::String
end
 
color(j::RoseQuartz) = "rosepink"
color(j::Jewel) = "Use the loupe."
color(j::Sapphire) = j.color
 
function testtypecopy()
    a = Sapphire("blue", 5.0, "good")
    b = RoseQuartz(3.5, "excellent")
 
    j::Jewel = deepcopy(b)
 
    println("a is a Jewel? ", a isa Jewel)
    println("b is a Jewel? ", a isa Jewel)
    println("j is a Jewel? ", a isa Jewel)
    println("a is a Sapphire? ", a isa Sapphire)
    println("a is a RoseQuartz? ", a isa RoseQuartz)
    println("b is a Sapphire? ", b isa Sapphire)
    println("b is a RoseQuartz? ", b isa RoseQuartz)
    println("j is a Sapphire? ", j isa Sapphire)
    println("j is a RoseQuartz? ", j isa RoseQuartz)
 
    println("The color of j is ", color(j), ".")
    println("j is the same as b? ", j == b)
end
 
testtypecopy()
 