G
G
Georgy Kuznetsov2021-05-03 08:57:19
OOP
Georgy Kuznetsov, 2021-05-03 08:57:19

Upcast and Downcast?

Help solve this dilemma forever.
Let's say the following code is written in pseudocode:

class A {

}

class B inherit from A {

}

class Program {
  public static void Main () {
    A first = new A();
    B second = new B();
    B third = new B();
    //1 Upcast or Downcast?
    third = first;
    //2 Upcast or Downcast?
    first = second; 
  }
}


And the question is, what is Upcast and what is Downcast?
On the Internet, even in English-language sources, instead of showing the answer in a specific code, as a rule, they immediately begin to climb into the jungle (although for good, before climbing there, you must first give an understanding of the process under discussion).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-05-03
@JoeSmith3100

Upcast - casting a child type to a parent type in the inheritance hierarchy.
Downcast - casting a parent type to a child type.
Upcast is always safe, because you can prove at the stage of writing the code that it is legal.

V
VitalyChaikin, 2021-05-03
@VitalyChaikin

https://ru.stackoverflow.com/questions/433314/Read...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question