In the Drosophila larval cardiac tube, aorta and heart differentiation are controlled by the Hox genes Ultrabithorax (Ubx) and abdominal A (abdA), respectively. There is evidence that the cardiac tube undergoes extensive morphological and functional changes during metamorphosis to form the adult organ, but both the origin of adult cardiac tube myocytes and the underlying genetic control have not been established. Using in vivo time-lapse analysis, we show that the adult fruit fly cardiac tube is formed during metamorphosis by the reprogramming of differentiated and already functional larval cardiomyocytes, without cell proliferation. We characterise the genetic control of the process, which is cell autonomously ensured by the modulation of Ubx expression and AbdA activity. Larval aorta myocytes are remodelled to differentiate into the functional adult heart, in a process that requires the regulation of Ubx expression. Conversely, the shape, polarity, function and molecular characteristics of the surviving larval contractile heart myocytes are profoundly transformed as these cells are reprogrammed to form the adult terminal chamber. This process is mediated by the regulation of AbdA protein function, which is successively required within these persisting myocytes for the acquisition of both larval and adult differentiated states. Importantly, AbdA specificity is switched at metamorphosis to induce a novel genetic program that leads to differentiation of the terminal chamber. Finally, the steroid hormone ecdysone controls cardiac tube remodelling by impinging on both the regulation of Ubx expression and the modification of AbdA function. Our results shed light on the genetic control of one in vivo occurring remodelling process, which involves a steroid-dependent modification of Hox expression and function.