Upcasting and subsequent downcasting allows you to access the attribute of the original type?

Asked

Viewed 164 times

3

When I make a upcasting, object 1 will be converted to its supertype and an object 2 is created

When object 1 is instantiated before doing the upcasting, has attributes that do not exist in the superclass, when creating object 2, through the upcasting, I will have in this object 2 only the attributes of the superclass.

In memory, when creating this object 2, in the heap is reallocated all components of object 1, and a reference to the copy" is created and a type reference object 2 is made, thus everything that had object 1 has in the heap on object 2, but I can only access the common part?

When there is superposition of superclass methods and in the subclass, how the compiler can know which to use, when there is this upcasting? I imagine the method that should count would be the super-class

2 answers

3


Upcasting|, At least in this situation, it is only a reinterpretation made by the compiler, he starts to understand what to access, without moving anything on the object. It is possible to have a copy of the object for some operation, but not for the casting. The copy will keep the same object, there will be no data loss, and the child class data can be accessed as long as a downcasting in the object.

Overlay is an ambiguous term. If a method is virtual and the child class override in it then is that method which will always be executed, provided that the object is of the daughter class.

If the method is not virtual then the method to be executed is the method of the type it is interpreting, even if the object is of the daughter class, if it is interpreting as the mother the method to be used will be that of the mother.

It is possible to write a code demonstrating all this:

using static System.Console;

public class Program {
    public static void Main() {
        var filha = new Filha();
        filha.y = 1;
        var mae = (Mae)filha;
        var filha2 = (Filha)mae;
        WriteLine(filha2.y);
        mae.Virtual();
        mae.Real();
        filha2.Virtual();
        filha2.Real();
    }
}

class Mae {
    public virtual void Virtual() => WriteLine("Mãe");
    public void Real() => WriteLine("Mãe");
}
class Filha : Mae {
    public int y;
    public override void Virtual() => WriteLine("Filha");
    public new void Real() => WriteLine("Filha");
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

1

I think there’s an example I’ve been able to better understand the overlap part of. There is a superclass geometric figures, within this superclass I have a method of area calculation. From this superclass, subclasses, rectangle class, triangle class and so on will be created, each with its area calculation method with the modifier override. When I create a geometric figure

Triangulo triangulo = new Triangulo();
Quadrado quadrado = new Quadrado();
FiguraGeometrica figuraGenerica = new FiguraGeometrica();
FiguraGeometrica figuraGeometrica1 = (FiguraGeometrica)triangulo;
FiguraGeometrica figuraGeometrica2 = (FiguraGeometrica)quadrado;

The area calculation method of the triangle is in figuraGeometrica1, because this was superimposed on the present method but is not in figuraGeometrica2, once the method superimposed on it is that of the square. Already in figuraGenerica the method of calculation of area is the way of superclass.

In the matter of memory, when there is the casting operation, down or up, no changes are made to the object, a reference, a new pointer is created pointing to the same object and the compiler interprets only that which is present in the superclass, that is, everything that had in the subclass object still exists, is not copied, only a new pointer in the stack is added pointing to the same object, so the compiler can access the fields present only in the superclass, but all other information is still present.

For example, within the Square class there is an attribute that is diagonal value, but this attribute does not exist in the superclass:

FiguraGeometrica figuraGeometrica2 = (FiguraGeometrica)quadrado;

in upcasting, a figure referenceGeometrica2 is created and points to the square that is in the Heap.

When performing a downcasting

Quadrado quadrado2 = (Quadrado)FiguraGeometrica2;

a new pointer is done, and it points to the same place, only now the compiler is able to access the diagonal attribute, which was not erased, was there all the time, in the same reference.

In short, the methods when casting is performed are the methods of the class itself, unless an overlap has been made. Attributes do not disappear, remain in the same reference only can no longer be accessed.

I believe that’s it !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.