Sure you can! 40 miles is definitely within reach!

I have a friend with a hybrid and I've taken her on 15-30 mile rides before (I have a road bike), and I could probaby push her farther if we rode longer distances regularly. She rides at a little slower pace and can't push up hills as efficiently, but I'm sure she could go the distance. She might not be happy with me afterward, but I know she could do it

Really, I'm not sure there's a limit to how far you can go, as long as you train. Sure, some things might be easier or better for longer distances on a road bike, but it's going to come down to training either way.