Ad Code

Responsive Advertisement

Basic Python Program - 53

Question 53: Python program to read and print students information using two classes using simple inheritance.
Download hole Program / Project code, by clicking following link:
List different types of inheritance ?
In Python, you can implement various types of inheritance to create class hierarchies and model relationships between classes. Here are some common types of inheritance:

In this program:
  1. Single Inheritance: In single inheritance, a class inherits from only one parent class. It's the simplest form of inheritance in Python.
    class Parent:
    pass
    class Child(Parent):
    pass
  2. Single Inheritance: In single inheritance, a class inherits from only one parent class. It's the simplest form of inheritance in Python.
    class Parent:
    pass
    class Child(Parent):
    pass
  3. Multiple Inheritance: Multiple inheritance allows a class to inherit from more than one parent class. This means a class can inherit attributes and methods from multiple sources.
    class Parent1:
    pass
    class Parent2:
    pass
    class Child(Parent1, Parent2):
    pass
  4. Multilevel Inheritance: Multilevel inheritance involves a chain of inheritance with multiple levels. A subclass inherits from another subclass, which in turn inherits from another, and so on.
    class Grandparent:
    pass
    class Parent(Grandparent):
    pass
    class Child(Parent):
    pass
  5. Hierarchical Inheritance: Hierarchical inheritance involves multiple subclasses inheriting from a single parent class. This creates a hierarchy where multiple classes share a common base class.
    class Parent:
    pass
    class Child1(Parent):
    pass
    class Child2(Parent):
    pass
  6. Hybrid (or Complex) Inheritance: Hybrid inheritance combines different types of inheritance. For example, a class can use a combination of single, multiple, multilevel, and hierarchical inheritance in a single class hierarchy.
    class A:
    pass
    class B(A):
    pass
    class C(A):
    pass
    class D(B, C):
    pass
  7. Cyclic Inheritance: Cyclic inheritance occurs when classes form a cycle of inheritance. Python allows this, but it can lead to ambiguous situations and should be avoided.
    class A:
    pass
    class B(A):
    pass
    class C(A):
    pass
    class D(B, C):
    pass
Each type of inheritance has its own use cases and considerations. Single and multiple inheritance are common and widely used. Multiple inheritance is a powerful feature in Python, but it can be complex to manage due to potential naming conflicts. Multilevel and hierarchical inheritance help create structured class hierarchies. Hybrid inheritance allows for more flexibility in modeling complex relationships.

It's essential to choose the appropriate type of inheritance based on your application's requirements and maintainability concerns.
Programming Code:
Following code write in: BP_P53.py
# Python Program

# Simple Inheritance
# Create Class

# Super / Parent class
class Student():

    # Attribute of Parent Class
    name = ""
    address = ""
    marks = None

    # Define Method
    def aboutME(self):
        print("I have studying in 10th Standard.")
    
# Subclass
class Class_10th(Student):
    # Subclass Method
    def display(self):
        print("Student Details")
        print("Student Name: ", self.name)
        print("Student Address: ", self.address)
        print("Student Marks: ", self.marks)

# Create object
obj = Class_10th()

# Superclass attributes & methods
obj.name = "Rahul Raj"
obj.address = "Pune, India"
obj.marks = 92

obj.aboutME()

# Subclass Method
obj.display()

# Thanks for Reading.
Output:

Post a Comment

0 Comments

Ad Code

Responsive Advertisement