o can refer to an Object, Foo is a subclass of Object so that is ok. The (Object) is a cast, but I do not think it's necesarry.
Foo foo = (Foo)o; \\ confusion 2 foo can refer to a Foo but o is not a reference to a Foo. For the compiler o just refers to an Object, even if we know it actually is a Foo. Hence here you need the (Foo) cast to make clear to the compiler that o actually refers to a Foo.
But I do not understand the title of your question, is my answer helping you with what you wanted to ask??
Line 6 : Cast is not necessary as Marc has pointed out. Line 7 : Cast is must, to compile the code, there wont be any problem at run time because the run time type of the o will be Foo object so assigning Foo object to Foo ref variable is OK.