DSA Practical 4
DSA Practical 4
2. Objects: An object is an instance of a class. It is a specific realization of the class with its own data and
behavior.
class Dog:
def bark(self):
def age_in_dog_years(self):
return self.age * 7
def display_info(self):
print(my_dog.age_in_dog_years()) # Output: 35
### Explanation
1. **Class Definition**:
- `__init__(self, name, age)`: The constructor method, called when a new object is created. It initializes
the `name` and `age` attributes of the object.
- `self.name` and `self.age`: These are attributes of the class. `self` refers to the current instance of the
class.
2. Creating an Object:
- `my_dog = Dog(name="Buddy", age=5)`: Creates an instance of the `Dog` class with the name
"Buddy" and age 5.
- `my_dog.bark()`: Calls the `bark` method, which returns a string indicating that the dog is barking.
- `my_dog.age_in_dog_years()`: Calls the `age_in_dog_years` method to convert the dog's age to dog
years.
- `my_dog.display_info()`: Calls the `display_info` method to get a formatted string with the dog's
information.
### Output
When you run the code, you'll get the following output:
35