Q:

Java type casting and scope

I have two interfaces: MinServer and MaxServer. MaxServer extends MinServer. So, is the following code normal and right?
MinServer server=foo.getUndefinedServer();
...code according to MinServer
if (isThisServerMax){
  server=(MaxServer)server;
...code according to MaxServer
}
...code according to MinServer

A:

No, it is not correct.
At this point:
server=(MaxServer)server
...code according to MaxServer

You are casting a MaxServer to a MinServer (which is OK) but then you assign the result of the casting to a MinServer ... so ehhm, you are in the same point where you started.
If you change it like this:
MaxServer server2 = (MaxServer)server
...code according to MaxServer using server2

then it will be correct.

