Classes and Objects
In a previous chapter we covered the theoretical aspects of OOP (object oriented programming). In this chapter we consider the practical topics of classes and objects which are the fundamental components of OOP.
A class is often, and probably best, described as a blueprint for building objects. Classes contain the details of the functionality and data of the objects that will be created based on the class. Consider the blueprint of a building. That document specifies all of the necessary details required for constructing the building. However, the blueprint is not the actual building and the building does not exist until it is constructed. Just as the class can be considered a blueprint, an object is the result of building using the specifications outlined by the class. Study the attributes and code below together to obtain a better understanding of the OOP concepts. If you are new to OOP don't be surprised by the bit of time and effort required to develop proficiency with the concepts.
Attributes of Classes and Objects
Classes and objects have the following attributes
- Class - describes the details of the objects that will be built based on the class.
- Class Identifiers - follow the naming convention of using uppercase for the starting letters of the words. Also, class names are usually nouns (e.g. Aircraft, Employee, Student, Faculty).
- Multiple Classes - A program can use multiple classes.
- Instantiation - When an object is created from a class it is known as instantiation. The object is instantiated from, or based on, a class.
- Instance - of the class is created and it is known as an object.
- Object Identifiers - follow the naming convention of variables (lowercase; underscores to separate words).
- Multiple Objects - can be instantiated from each class.
- Functionality and Data - Objects contain functionality and data. Methods represent the functionality in an object. Data members represent the data in an object.
- Constructor (Initializer) - The method named '__init__' is known as the constructor or initializer. It is called automatically when an object is instantiated.
- Member Access - access object methods and data using the dot '.' operator. For example, 'child_object.set_attribute(200)' is calling the 'set_attribute()' method of the 'child_object'.
- Inheritance - Classes can inherit functionality and data from other classes.
- Getter - (accessor) member functions 'get' (return) data from objects.
- Setter - (mutator) member functions 'set' data in objects.
- Class Variables - are declared in the class but outside of all methods and are shared by all objects instantiated of that class.
- Instance Variables - are variables declared inside methods of the class. Each object of the class gets its own copy of an instance variable.
- Reduce Visibility to Variable - To make an instance variable visible only to members of the class use the double underscore prefix (e.g. self.__job_title).
- Self - instance methods must specify an object identifier that is used to reference instance variables. This object is usually called 'self' although it can be any identifier. Observe in the example below how 'self' is used to refer to the instance variables: self.name, self.__job_title. The 'self' object refers to the object that is calling the method.
- Instance Method - the default method of a class. Includes an implicit parameter 'self' in the method definition but not in the call. Instance methods are called via class instances (objects).
- Class Method - identified by the decorator @classmethod. Includes an implicit parameter 'cls' in the method definition but not in the call. cls will be set to the name of the class calling the class method. Class methods are called via the class (but can also be called by an instance).
- Static Method - identified by the decorator @staticmethod. Static methods do not include an implicit parameter. Static methods can be called via either an instance (object) or the class.
In the first code example, two objects are instantiated from the Employee class. This example includes code for class-level variables, instance variables, and member methods.
Here's the output.
In the second code example, the concept of inheritance is demonstrated. There are three classes: Parent, Child, and Grandchild. An object of the Child class is created named child_object which inherits from the Parent class. This means that all members of the Parent class are available to each object created from the Child class. An object of the Grandchild class is created named grandchild_object which inherits from the Child class. Since the Child class inherits from Parent and Grandchild inherits from Child, all of the members of both Parent and Child are available to each object created from the Grandchild class.
Here's the output.
In the next code example, the concepts of instance, class, and static methods are demonstrated.
Here's the output.