12. Problem to create a class with hierarchical inheritance in Python

In this Python oops program, we will create a class with hierarchical inheritance in Python. The program illustrates inheritance in Python with multiple child classes inheriting from a single-parent class.

Create a class with hierarchical inheritance

Steps to solve the program
  1. The ParentClass is a base class or parent class that defines a method called parent_method. This method prints “Parent method”.
  2. The ChildClass1 is a derived class or child class that inherits from ParentClass. It defines its own method called child_method1, which prints “Child 1 method”.
  3. The ChildClass2 is another derived class or child class that also inherits from ParentClass. It defines its own method called child_method2, which prints “Child 2 method”.
  4. Two objects, obj1 and obj2, are created using the ChildClass1 and ChildClass2, respectively.
  5. The obj1.parent_method() call invokes the parent_method inherited from ParentClass, which prints “Parent method”.
  6. The obj1.child_method1() call invokes the child_method1 defined in ChildClass1, which prints “Child 1 method”.
  7. Similarly, the obj2.parent_method() call invokes the parent_method inherited from ParentClass, which prints “Parent method”.
  8. The obj2.child_method2() call invokes the child_method2 defined in ChildClass2, which prints “Child 2 method”.

				
					class ParentClass:
    def parent_method(self):
        print("Parent method")

class ChildClass1(ParentClass):
    def child_method1(self):
        print("Child 1 method")

class ChildClass2(ParentClass):
    def child_method2(self):
        print("Child 2 method")

# Create objects of the child classes
obj1 = ChildClass1()
obj2 = ChildClass2()

obj1.parent_method()
obj1.child_method1()

obj2.parent_method()
obj2.child_method2()
				
			

Output:

				
					Parent method
Child 1 method
Parent method
Child 2 method
				
			

Related Articles

Python Class with Multilevel Inheritance.

Python Class with Method Overloading.

11. Problem to create a class with multilevel inheritance in Python

In this Python oops program, we will create a class with multilevel inheritance in Python. The program showcases inheritance hierarchy in Python. The ChildClass inherits methods from both ParentClass and GrandparentClass, and an object created from the child class can access and invoke methods from all the ancestor classes as well as its own methods.

Create a class with multilevel inheritance

Steps to solve the program
  1. The GrandparentClass is a base class or parent class that defines a method called grandparent_method. This method prints “Grandparent method”.
  2. The ParentClass is a derived class or child class that inherits from GrandparentClass. It also defines its own method called parent_method, which prints “Parent method”.
  3. The ChildClass is another derived class or child class that inherits from ParentClass. It defines its own method called child_method, which prints “Child method”.
  4. An object obj is created using the ChildClass. Since ChildClass inherits from ParentClass, and ParentClass inherits from GrandparentClass, the object obj has access to the methods defined in all three classes.
  5. The obj.grandparent_method() call invokes the grandparent_method inherited from GrandparentClass, which prints “Grandparent method”.
  6. The obj.parent_method() call invokes the parent_method inherited from ParentClass, which prints “Parent method”.
  7. The obj.child_method() call invokes the child_method defined in the ChildClass, which prints “Child method”.

				
					class GrandparentClass:
    def grandparent_method(self):
        print("Grandparent method")

class ParentClass(GrandparentClass):
    def parent_method(self):
        print("Parent method")

class ChildClass(ParentClass):
    def child_method(self):
        print("Child method")

# Create an object of the child class
obj = ChildClass()
obj.grandparent_method()
obj.parent_method()
obj.child_method()
				
			

Output:

				
					Grandparent method
Parent method
Child method
				
			

Related Articles

Python Class with Multiple Inheritance.

Python Class with Hierarchical Inheritance.

10. Problem to create a class with multiple inheritance in Python

In this Python oops program, we will create a class with multiple inheritance. The program demonstrates multiple inheritance in Python. The ChildClass inherits methods from both ParentClass1 and ParentClass2, and an object created from the child class can access and invoke methods from all the parent classes as well as its own methods.

Class with multiple inheritance

Steps to solve the program
  1. In this program, we demonstrate multiple inheritance in Python, where a child class inherits from multiple parent classes.
  2. The ChildClass inherits both the parent_method1 from ParentClass1 and the parent_method2 from ParentClass2. This allows the object obj of the ChildClass to access and invoke methods from both parent classes, as well as its own class method.
  3. Multiple inheritance provides flexibility in reusing code from multiple sources and allows for complex class hierarchies.
  4. The program output will be:It first prints “Parent method 1” because obj.parent_method1() invokes the method from ParentClass1. Then it prints “Parent method 2” because obj.parent_method2() invokes the method from ParentClass2. Finally, it prints “Child method” because obj.child_method() invokes the method from the child class.

				
					class ParentClass1:
    def parent_method1(self):
        print("Parent method 1")

class ParentClass2:
    def parent_method2(self):
        print("Parent method 2")

class ChildClass(ParentClass1, ParentClass2):
    def child_method(self):
        print("Child method")

# Create an object of the child class
obj = ChildClass()
obj.parent_method1()
obj.parent_method2()
obj.child_method()
				
			

Output:

				
					Parent method 1
Parent method 2
Child method
				
			

Related Articles

Python class with Single Inheritance.

Python Class with Multilevel Inheritance.

9. Problem to create a class with single inheritance in Python

In this Python oops program, we will create a class with single inheritance. The program demonstrates inheritance in Python. The ChildClass inherits the parent_method from the ParentClass and adds its own method child_method

Class with single inheritance

Steps to solve the program
  1. In this program, we demonstrate single inheritance in Python, where a child class inherits from a single parent class.
  2. The ChildClass inherits the parent_method from the ParentClass, allowing the object obj of the ChildClass to access and invoke both the parent class method (parent_method) and its own class method (child_method).
  3. This concept of inheritance allows for code reuse and enables the child class to extend or override the functionality of the parent class.
  4. The program output will be: It first prints “Parent method” because obj.parent_method() invokes the method from the parent class. Then it prints “Child method” because obj.child_method() invokes the method from the child class.

				
					class ParentClass:
    def parent_method(self):
        print("Parent method")

class ChildClass(ParentClass):
    def child_method(self):
        print("Child method")

# Create an object of the child class
obj = ChildClass()
obj.parent_method()
obj.child_method()
				
			

Output:

				
					Parent method
Child method
				
			

Related Articles

Python Class object under syntax if __name__ == ‘__main__’.

Python Class with Multiple Inheritance.

8. Problem to write Python class under the given syntax

In this Python oops program, we will write python class under syntax if __name__ == ‘__main__’. The program demonstrates the usage of a class with a constructor and a method. It creates an object, initializes its name attribute, and displays the name using the display_name method. The code is enclosed in an if __name__ == '__main__': block to ensure it only runs when the script is executed directly.

Write Python class under given syntax

Steps to solve the program
  1. The code demonstrates the use of the __name__ variable in Python, which provides information about the current module’s name.
  2. By using the if __name__ == ‘__main__’: condition, we can ensure that certain code within the block is executed only when the module is run directly as the main program.
  3. In this case, the MyClass class is defined, and an object obj is created with the name “Jason” passed as an argument to the constructor.
  4. The display_name method is then called on the obj object, which prints the name stored in the instance variable self.name.
  5. The use of if __name__ == ‘__main__’: is a common practice in Python to separate code that should only run when the module is run as the main program from code that should not be executed when the module is imported as a module in another program.

				
					class MyClass:
    def __init__(self, name):
        self.name = name
    
    def display_name(self):
        print("Name:", self.name)

if __name__ == '__main__':
    obj = MyClass("Jason")
    obj.display_name()
				
			

Output:

				
					Name: Jason
				
			

Related Articles

Python Class to get the class name and module name.

Python class with Single Inheritance.

7. Problem to create Python class to get class and module name

In this Python oops program, we will create python class to get class and module name. The program demonstrates how to retrieve the class name and module name of an object in Python.

Create Python class to get class and module name

Steps to solve the program
  1. The code demonstrates how to get the class name and module name of an object in Python.
  2. After creating an object obj of the MyClass class, we use the __class__.__name__ attribute to get the name of the class to which obj belongs. This attribute returns a string containing the class name.
  3. The __module__ attribute, on the other hand, returns the name of the module where the class is defined. In this case, since the code is executed directly without being imported as a module, the __module__ attribute will return “__main__” indicating that the class is defined in the main module.
  4. By printing the class name and module name, we can observe the names associated with the object obj of the MyClass class.

				
					class MyClass:
    pass

obj = MyClass()
print("Class name:", obj.__class__.__name__)
print("Module name:", obj.__module__)
				
			

Output:

				
					Class name: MyClass
Module name: __main__
				
			

Related Articles

create a class with the class method.

Python Class object under syntax if __name__ == ‘__main__’.

6. Problem to create Python class with class method

In this Python oops program, we will create a Python class with class method. The program demonstrates the usage of a Python class with class method. The class_method is defined within the MyClass class and can be called directly on the class itself. It accesses and prints the value of the class variable class_var.

Python class with class method

Steps to solve the program
  1. The code demonstrates the usage of a class method within a class in Python.
  2. Class methods are defined using the @classmethod decorator before the method definition. They receive the class itself as the first parameter, conventionally named cls. Class methods are commonly used when the method needs to access or modify class-level variables or perform some operation specific to the class itself.
  3. In this case, the class_method() is a method that prints the value of the class_var class variable when called. The class variable can be accessed using cls.class_var, where cls refers to the class itself.
  4. To invoke a class method, we use the class name followed by the method name, like MyClass.class_method(). Since class methods are associated with the class itself, they can be called without creating an object of the class.
  5. Class methods are useful when you want to define methods that are related to the class as a whole and not specific to any particular instance. They can access class variables and perform operations that affect the class itself.

				
					class MyClass:
    class_var = "Hello"
    
    @classmethod
    def class_method(cls):
        print("Class variable:", cls.class_var)

MyClass.class_method()
				
			

Output:

				
					Class variable: Hello
				
			

Related Articles

create a class with a static method.

Python Class to get the class name and module name.

5. Problem to create a Python class with static methods

In this Python oops program, we will create a Python class with static methods. The program demonstrates the usage of a static method in Python. The static method static_method is defined within the MyClass class and can be called directly on the class itself.

Python class with static methods

Steps to solve the program
  1. The code demonstrates the usage of a class with static methods in Python.
  2. Static methods are defined using the @staticmethod decorator before the method definition. They do not receive any special first parameter like self or cls. Therefore, they do not have access to the instance or class variables.
  3. In this case, the static_method() is a simple method that prints the string “This is a static method” when called.
  4. To invoke a static method, we use the class name followed by the method name, like MyClass.static_method(). Since static methods are associated with the class itself, they can be called without creating an object of the class.
  5. Static methods are commonly used when a method does not require access to instance or class variables, and its behavior is independent of the specific instances of the class. They provide a way to encapsulate utility or helper functions within a class.
				
					class MyClass:
    @staticmethod
    def static_method():
        print("This is a static method")

MyClass.static_method()
				
			

Output:

				
					This is a static method
				
			

Related Articles

create a class with class variables.

create a class with the class method.

4. Problem to create a Python class with class variables

In this Python oops program, we will create a Python class with class variables. The program demonstrates the usage of a class variable in Python.

Python class with class variables

  1. The code demonstrates the usage of a Python class with class variables.
  2. A class variable is a variable that is shared by all instances of a class. It is defined within the class but outside any methods.
  3. In this case, the class_var class variable is assigned the value “Hello” within the MyClass class definition.
  4. By accessing MyClass.class_var, we can directly retrieve and print the value of the class_var class variable, which will output “Hello” in this case.
  5. Class variables are useful when you want to define attributes that are common to all instances of a class.
  6. They are shared among all instances and can be accessed without creating an object of the class.

				
					class MyClass:
    class_var = "Hello"

print(MyClass.class_var)
				
			

Output:

				
					Hello
				
			

Related Articles

create a class with Instance methods.

create a class with a static method.

3. Problem to create a Python class with instance method

In this Python oops program, we will create a Python class with instance method. The program creates a class with a constructor and methods to display and update the name attribute of an object. It creates an object, displays its initial name, updates the name, and then displays the updated name.

Python class with instance method

Steps to solve the program
  1. The code demonstrates how to create a Python class with instance method and variables for updating and displaying the value of an attribute.
  2. The constructor method __init__() is used to initialize the name instance variable when an object is created.
    The display_name() method allows us to print the value of the name instance variable.
  3. The update_name() method provides a way to update the value of the name instance variable by passing a new name as a parameter.
  4. By combining these methods, we can create objects of the MyClass class, set and update the name attribute, and display the updated name whenever needed. In the example, the name is initially set to “Omkar” and then updated to “Ketan”.
				
					class MyClass:
    def __init__(self, name):
        self.name = name
    
    def display_name(self):
        print("Name:", self.name)
    
    def update_name(self, new_name):
        self.name = new_name

# Create an object of the class
obj = MyClass("Omkar")
obj.display_name()

obj.update_name("Ketan")
obj.display_name()
				
			

Output:

				
					Name: Omkar
Name: Ketan
				
			

Related Articles

create a class with an instance variable.

create a class with class variables.