Junior — Middle
How are the classes Person, Man, and Woman related in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming, classes "Person", "Man", and "Woman" are usually related through inheritance. The "Person" class acts as a base (parent) class, containing common properties and methods characteristic of all people, such as name, age, speaking or walking methods.
The "Man" and "Woman" classes inherit from the "Person" class and can add properties or behaviors specific to each gender. This helps avoid code duplication and structures the program logically.
Example in Python:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
print(f"Hello, my name is {self.name}.")
class Man(Person):
def __init__(self, name, age):
super().__init__(name, age)
self.gender = "man"
class Woman(Person):
def __init__(self, name, age):
super().__init__(name, age)
self.gender = "woman"
Thus, "Man" and "Woman" are specialized versions of the "Person" class.