class Dog:
def __init__(self, name):
"""
class constructor
Parameters
----------
name : str
The name of the dog
"""
self.name = name # public attribute
self.__age = 20 # private attribute
def bark(self):
"""
Class method describing the behavior of the dog (class)
"""
print("Woof 🐶🐶!")
def get_age(self):
"""
Class method to get the age of the dog
Because the age of the dog is private, we need a method to get it
"""
return self.__age
def set_age(self, age):
"""
Class method to set the age of the dog
Because the age of the dog is private, we need a method to set it
"""
self.__age = age
def __str__(self):
"""
Class method to get the name of the dog
it is invoked when we print the object withouth calling the name attribute or a specific method
"""
return self.name
class Puppy(Dog):
pass
class Fox(Dog):
def bark(self):
print("Ring-ding-ding-ding-dingeringeding 🐺🐺🐺!")
greek_hound = Dog("Greek Hound")
print("public Dog Name: ", greek_hound.name)
greek_hound.bark()
print("Private Dog Age: ", greek_hound.get_age())
greek_hound.set_age(3)
print("Updated Dog Age: ", greek_hound.get_age())
greek_hound.__age = 4 # this will not change the age of the dog
print("Failed Attempt to update age: ", greek_hound.get_age())
puppy = Puppy("Puppy")
print("public Puppy Name: ", puppy.name)
puppy.bark()
print("Private Puppy Age: ", puppy.get_age())
puppy.set_age(1)
print("Updated Puppy Age: ", puppy.get_age())
puppy.__age = 2 # this will not change the age of the puppy
print("Failed Attempt to update age: ", puppy.get_age())
fox = Fox("Fox")
print("public Fox Name: ", fox.name)
fox.bark()
The documentation includes the class Dog which has a public attribute name and a private attribute __age. It also includes the class methods bark(), get_age(), set_age(), and str(). The class Puppy inherits from the Dog class and the Fox class overrides the bark() method from the Dog class.
The code demonstrates creating objects from the Dog, Puppy, and Fox classes, accessing their attributes and methods, and updating the private age attribute using the `set_age