python
python
# Method to display dog's info 3. **Multilevel Inheritance**: A class inherits from a class
def info(self): which is also inherited from another class.
print(f"Dog Name: {self.name}, Age: {self.age}") ```python
2. Instan a ng an Object: class Grandfather:
o Create an instance of the Dog class by calling it as if it def trait(self):
were a func on. return "Wisdom"
o The __init__() method is automa cally called to ini alize
the object's a ributes. class Father(Grandfather):
python def father_trait(self):
my_dog = Dog("Buddy", 3) # Crea ng an object of the Dog return "Strength"
class
class Son(Father):
# Calling methods on the object pass
my_dog.bark() # Output: Buddy says Woof!
my_dog.info() # Output: Dog Name: Buddy, Age: 3 son = Son()
Explana on print(son.trait()) # Output: Wisdom
Class Defini on: print(son.father_trait()) # Output: Strength
o __init__ Method: This is a special method called a ```
constructor. It ini alizes the
object's a ributes. The self parameter is a reference to the 4. **Hierarchical Inheritance**: Multiple classes inherit from
current instance of the the same superclass.
class. ```python
o Other Methods: Methods like bark and info define the class Animal:
behaviors of the objects. def sound(self):
Object Instan a on: return "Some sound"
o Crea ng an Object: my_dog = Dog("Buddy", 3) creates an
instance of the Dog class class Dog(Animal):
with name as "Buddy" and age as 3. def sound(self):
o Accessing Methods: You can call the methods of the class return "Bark"
on the object using the
dot nota on (e.g., my_dog.bark()). class Cat(Animal):
Benefits of Using Objects def sound(self):
Modularity: Classes and objects promote modularity and return "Meow"
reusability of code.
Encapsula on: Objects encapsulate data and behavior, dog = Dog()
which can help in organizing and cat = Cat()
managing code. print(dog.sound()) # Output: Bark
Inheritance: Classes allow for inheritance, making it print(cat.sound()) # Output: Meow
easier to create subclasses that extend ```
the func onality of base classes.
5. **Hybrid Inheritance**: A combination of two or more
write a program that reads the content of the file types of inheritance. It can be achieved by mixing any of
and count the occurences of the each letter promt the above types.
the user to enter the file name ```python
class Grandparent:
def count_letters(file_name): def grandparent_trait(self):
# Initialize a dictionary to keep track of letter counts return "Wisdom"
letter_counts = {}
class Parent(Grandparent):
try: def parent_trait(self):
with open(file_name, 'r') as file: return "Kindness"
content = file.read().lower()
class Uncle(Grandparent):
# Loop through each character in the file content def uncle_trait(self):
for char in content: return "Humor"
if char.isalpha():
if char in letter_counts: class Child(Parent, Uncle):
letter_counts[char] += 1 pass
else:
letter_counts[char] = 1 child = Child()
print(child.grandparent_trait()) # Output: Wisdom
# Print the letter counts print(child.parent_trait()) # Output: Kindness
for letter, count in sorted(letter_counts.items()): print(child.uncle_trait()) # Output: Humor
print(f"{letter}: {count}")
except FileNotFoundError:
print(f"The file {file_name} was not found.")
if __name__ == "__main__":
# Prompt the user to enter the file name
file_name = input("Enter the file name: ")
count_letters(file_name)
```python
import os
def get_file_size(file_name):
try:
file_size = os.path.getsize(file_name)
print(f"The size of '{file_name}' is {file_size} bytes.")
except FileNotFoundError:
print(f"The file '{file_name}' was not found.")
if __name__ == "__main__":
# Prompt the user to enter the file name
file_name = input("Enter the file name: ")
get_file_size(file_name)
```
class Dog(Animal):
def sound(self):
return "Bark"
dog = Dog()
print(dog.sound()) # Output: Bark
```
class Mother:
def mother_trait(self):
return "Eye color"