In the first part of this tutorial we introduced a class as a definition of a structure containing both its own data and its own functionality. Well designed classes are pretty much self contained and should not rely on data external to the class to work. Our Rectangle class held all the data it needed (position, height, width, color) in its properties stored within the class. All the methods that operate on that data are also contained within the class. You could take the Rectangle Class file from our Paint program and copy it to a different graphics program and use it without any problem. It did not rely on anything else from the original program to work.
This idea of encapsulation is very important in object oriented programming. From a practical point of view it allows code to be easily re-used in different programs and applications. You can build a library of classes and use it again and again – even give (or sell) it to other developers, and use (or buy) class libraries from others.
It also makes testing and debugging your code much easier. A class defines an object and it just does what that object does and nothing else, and it does it (should do it) very well. Its functionality is well defined and very limited in scope, so it is easy to test thoroughly and (fairly) easy to see where things are going wrong when they do.