What is a class library (in object-oriented programming)?
In object-oriented programming, a class library is a collection of classes and other reusable software components, such as interfaces and value types. Developers can import class libraries or their components into their applications and use the prewritten code to carry out specific tasks.
A class library -- or, simply, library -- is analogous to a subroutine library in earlier procedural programming.
After importing a class library into an application, a developer can instantiate objects -- create real instance of them -- based on the classes within the library. The developer can then use those objects to access the methods and properties available to the classes.
Example of object-oriented programming in action
For example, python-docx is an open source Python library that a developer or user can utilize to create and update Microsoft Word documents. After installing the library package on a system, the developer can import it into their Python scripts by issuing an import docx statement. They can then use the docx namespace to access the classes within the library. Figure 2 shows a simple Python script that demonstrates how to use the python-docx library to create a Word document and add initial content.
The script first imports the python-docx library and then defines the full path and file name for the new document, saving the path as a string to the test_doc variable. Next, the script instantiates an object based on the Document class in the docx namespace and assigns the object to the doc variable.
The doc variable is then used to call the add_heading and add_paragraph methods in the Document class. The add_heading method takes two arguments: the heading text and the heading level. The top-level heading is 0, the second-level heading is 1 and so on. The add_paragraph method also takes two arguments: the paragraph text and the paragraph style, which, in this case, is Normal. The paragraph text is passed into the method through the intro and section variables.
After adding the two headings and two paragraphs, the script uses the doc variable to call the save method in the Document class. The test_doc variable is passed in as an argument to the method. When you run the script, Python creates the specified Word document with the added content, as shown in Figure 3.
If the python-docx library -- or something comparable -- were not available, every Python developer who needed to create or update a Word document in their scripts would have to spend the time necessary to develop these components themselves, even if they were all trying to achieve the same results. They could try sharing their code through other mechanisms, but these approaches often come with their own challenges.
How do class libraries simplify the job of the developer?
Class libraries greatly simplify the developer's job by providing access to code that performs specific tasks without having to invest in the time and effort needed to write the code from scratch. Many libraries are also available as open source projects, so they can be customized to meet a developer's or organization's specific requirements, which can be especially beneficial if using the library in multiple applications.
Programming languages typically include core class libraries that developers can access within their applications. Python, for example, comes with the Standard Library, which contains numerous built-in modules that are written in either C or Python and that provide access to a wide range of classes.
Another example is the Java Class Library, which includes a large collection of classes organized into packages of similar functionality. The packages are available at runtime to applications running in a Java virtual machine. Microsoft's .NET Framework also offers an assortment of class libraries that are available as either base class libraries that provide core capabilities or as framework class libraries that deliver a more complete set of classes.