For two bar magnets, when opposite poles come closer they attract while like poles N-N or S-S repel each other. This happens due to the force of attraction and repulsion between the field lines. For a solenoid, if seen from one side, for the clockwise current the south pole is formed and vice versa. This is determined with the help of right hand thumb rule.