Adobe did an amazing job of making sure Flash was there, and seamless. To the point people barely had an awareness.
Java Applets failed because Sun was a 'debating society' (per James Gosling). Definitely not a consumer oriented company, thinking about end user behaviour etc..
Other reasons as well, but that's #1.
Source: me, deployed a bunch of Enterprise projects on Applets back in the day.
If you look at Java's security track record in recent years, there's been just a couple of zero days in the past five years. That's probably a mix of genuinely better security and less attention due to being kicked out of web browsers. But if you go look at the security histories of other sandboxes like browsers or kernels, it starts to look pretty good. A new paper that just came out introduced a new Linux kernel fuzzer and discovered, I think, over 30 zero day exploits across several different Android phones. So ordinary UNIX style process isolation is pretty useless if you can reach device drivers from inside the processes. Even the quite aggressive and resource intensive browser sandboxes that browsers use routinely have escapes, often because they're always adding large new attack surfaces like WebAssembly, WebGL etc (all written in C++).
There are inevitably defects in the design and/or implementation which will open up browsers to gaping security vulnerabilities until they are found and fixed.