How this equation of the voltage is attained?
To find voltage between \$A\$ and \$B\$ , traverse from node B to A through any of the paths, and add all the voltage drops along the way. For instance, you can follow this path:

P.S: Assuming that the circuit and current directions are correct, you will find that the textbook answer is wrong for \$V_{AB}\$.