Class and Object

Class and Object

  • Class: a mapping between computer programs and the entities from real/thinking realm. It’s a blueprint that defines a set of attributes that will characterize any object that is instantiated from this class.

  • Class encapsulates data (variables) and operations (methods) on those data

  • A good class design depends on behavior (method) and attribute (variables). Behaviors need to be relevant with the class

  • For example, for the class definition below, method print_file shouldn’t belong to Student, rather it should belong to Printer

class Student():
    name = ""
    age = 0

    def print_file(self):
        print("name: {} \n age: {}".format(self.name, str(self.age)))
  • Rather, define print_file in a class called Printer
class Printer():
    name = ""
    age = 0

    def print_file(self):
        print("name: {} \n age: {}".format(self.name, str(self.age)))
  • class is abstract whereas object is a specific instance of a class

  • Object: An instance of a class. This is the realized version of the class, where the class is manifested in the program.