People often say that Macs are more secure than say Windows. Which is why many Mac users don't have antiviruses. But why is this? Is it because of the need for a password for sudo commands, like changing system files?
But then couldn't the hacker just steal the password also in his virus - how hard could that be? Why is Mac more secure than Windows?
There are fewer attacks and malware on Mac OS X systems for a variety of reasons, almost none of which having any relation with the notion of "software quality":
There are fewer OS X systems than Windows systems (right now, about 7.5% of computers involved in Internet browsing use OS X, according to StatCounter), making OS X a less interesting target for malware authors. Developing malware costs efforts and time, so the malware authors want to get a good return over investment. So they hunt where there are the most preys, i.e. on Windows, not OS X.
Malware usually enters a machine through explicit user action, i.e. installation of some software package that turned out to be dodgy. OS X users tend to install less third-party packages, mostly because OS X comes out-of-the-box with more accessories.
In a similar vein, when it comes to supporting hardware, OS X has its own drivers, so the normal Mac user plugs his new device: either it works right away, or it will never work. There is usually little point, in the OS X world, to try to download and install third-party drivers. On the other hand, for Windows, driver download is the norm. This does not mean that third-party drivers are often infected, but it implies that Windows users are accustomed to downloading executables and running them with full administrator rights.
Still on the same subject, Windows has long suffered from something known as DLL Hell, coming from the traditional method of identifying DLL only by a short file name, with no versioning support. This has trained many Windows users to "solve issues" by looking for the faulty DLL name on Google and then downloading it from the first link that arises. OS X, on the other hand, is on the habit of embedding needed libraries in the application, which avoids the issue (at the expense of disk space, since it makes applications larger -- that's rarely a problem with today's disks).
The main characteristic of Windows is its backward compatibility: that which was once running in any version of Windows, will keep running. This has made the system API very complex through decades of accumulation; and this complexity helps malware authors in the hide-and-seek game that they play with antivirus authors. Backward compatibility is much less maintained in the Mac OS world (Apple even switched complete architectures several times, switching from Motorola 680x0 CPU to PowerPC, and then to Intel x86; and support of 32-bit x86 application code can be predicted to be removed at some point).
It must still be said that what makes a machine secure is not really its software, but its system administration, i.e. what the user makes with the machine. Most of the points above are about how OS X users do not behave the same way as Windows users, for a lot of reasons, some of them being historical.
It is not always true that this operating system is safer than other. It depends for example on the versions you compare. Ir also depends on the factors used to assess this OS is better or worse than an other. For example, the delay taken for an OS to patch a security vulnerability may impact the image of the OS to users as it occured to Mac as stated on the third link above. Even the idea that Windows OS is more targeted than other systems is not always true:
Estimates suggest that 700,000 Mac OS X users suffered from the
Flashback Trojan virus . Users that had believed they didn’t need
antivirus software – because they were Mac users – found that their
false sense of security offered no defense against the attack.