Ad Code

Responsive Advertisement

Basic Python Program - 52

Question 52: Create a class Employee with data members: name, department and salary. Create suitable methods for reading and printing employee information.
Download hole Program / Project code, by clicking following link:
State the use of inheritance ?
Inheritance is a fundamental concept in object-oriented programming, and it plays a crucial role in Python. In Python, inheritance allows you to create a new class that is based on an existing class, inheriting its attributes and methods. Here are some of the primary uses and advantages of inheritance in Python:

In this program:
  1. Code Reusability: Inheritance allows you to reuse the code defined in a parent class. You can create a new class that inherits the properties and behaviors of an existing class. This promotes code reuse and reduces redundancy.
  2. Hierarchy and Structure: Inheritance helps you create a hierarchical structure for your classes, allowing you to model real-world relationships effectively. For example, you can represent a "Vehicle" class and create subclasses like "Car" and "Bicycle" that inherit common properties from the "Vehicle" class.
  3. Extensibility: Inherited classes can be extended by adding new attributes and methods. You can create subclasses that add functionality specific to the subclass while inheriting the common functionality from the parent class.
  4. Polymorphism: Inheritance is closely tied to the concept of polymorphism, which allows objects of different classes to be treated as objects of a common base class. This enables you to write code that can work with objects of various related classes without knowing their exact types.
  5. Method Overriding: Inherited methods can be overridden in a subclass. This allows you to provide specialized implementations of methods in the subclass, tailoring the behavior to the specific needs of the subclass.
  6. Organizing Code: Inheritance helps organize and structure your code. You can create a well-defined and logical class hierarchy that reflects the relationships between different entities in your application.
  7. Reduced Maintenance: When you need to make changes or enhancements to a shared piece of functionality, you can do it in the parent class, and those changes will automatically apply to all the subclasses, reducing maintenance efforts.
  8. Abstraction: Inheritance allows you to create abstract base classes that define a common interface without providing concrete implementations. Subclasses then provide the concrete implementations of these methods.
In Python, you define inheritance by creating a subclass that specifies the parent class it inherits from within the class definition. For example:
class ParentClass:
         # Attributes and methods of the parent class
  class ChildClass(ParentClass):
         # Additional attributes and methods of the child class

The child class (ChildClass) inherits the attributes and methods of the parent class (ParentClass) and can provide its own attributes and methods. This is the essence of inheritance in Python, and it allows you to create a rich and organized class hierarchy in your code.
Programming Code:
Following code write in: BP_P52.py
# Python Program
# Create Class

class Employee():
    # Define Mehod

    # Initialize values
    # def __init__(self, name = chr, department = chr, salary = int):
    #     self.name = name
    #     self.department = department
    #     self.salary = salary
    
    def read(self):
        self.name = input("Enter Employee Name: ")
        self.department = input("Enter Department Name: ")
        self.salary = int(input("Enter Employee Salary: "))

    # Display Details
    def output(self):
        print("**************** Employee Details ****************")
        print("Employee Name: ", self.name)
        print("Employee Department: ", self.department)
        print("Employee Salary: ", self.salary ," RS")

# Create class object
emp = Employee()

# Call Method
# emp.__init__("Rahul", "Software Developer", 50000)

# Read values Method
emp.read()

# show details Method
emp.output()

# Thanks for Reading.
Output:

Post a Comment

0 Comments

Ad Code

Responsive Advertisement