Union of connected subsets$X$,$Y$ is connected if $\overline{X}\cap Y \neq \varnothing$

I'm trying to prove the following :

Let A be a topological space an let $X$ and $Y$ be to connected subsets with $\overline{X}\cap Y \neq \varnothing$ with $\overline{X}$ the closure of $X$ . Show that $X\cup Y$ is connected.

I first tried to look at the case $X\cap Y \neq \varnothing$ and prove it by contradiction. If $X\cup Y$ is disconnected it can be written as $U\cup V$ with $U$ and $V$ open and disjoint. So we have

$X\cup Y = U\cup V$

$(X\cup Y)\cap Y = (U\cup V)\cap Y$

$(X\cap Y)\cup(Y\cap Y) = (U\cup V)\cap Y$

$Y =Y\cap (U\cup V)$ so $Y \subset (U\cup V)$ and $Y$ is disconnected.

Since $X$ is connected $\overline{X}$ is also connected and I thought the proof could be modified but I didn't see how.

Let $f:X\cup Y\rightarrow \{0,1\}$ be a continuous function. The restriction of $f$ to $X$ is constant since $X$ is connected.Suppose that it is $1$. $f^{-1}(1)$ is closed and contains $X$, so it contains $\bar X$. We deduce that the restriction of $f$ to $\bar X\cap Y$ is $1$. Since $Y$ is connected, the restriction of $f$ to $Y$ is also constant It is equal to the restriction of $f$ to $\bar X\cap Y$ which is $1$. Thus $f$ is constant and $X\cup Y$ is connected.