r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

Show parent comments

18

u/procrastinatingcoder Apr 05 '22

All dogs are animals, not all animals are dogs. Logically you can go one way, not the other.

More specifically, I'm not sure of what bytecode gets generated for the JVM, but as far as C++ and a few other compiled languages go, what "inherited" classes do is just "copy" the inherited code with their own code right beside it. So you can always refer to something you copied, but you can't generate the missing pieces afterwards.

2

u/paulstelian97 Apr 06 '22

For the bytecode, upcasting is essentially a no-op or assignment. Down casting involves a manual dynamic type check (and throwing an exception if the check fails) followed by a simple assignment (or a no-op again)