Implement a class Planet with basic attributes like name, size, and distance_from_sun. Then define child classes for different planets of the solar system, each with unique properties and methods.
Example 1:
Input: Earth('Earth', '12742 km', '147.09 million km', '7.5 billion')
Output: "Earth has a size of 12742 km and is 147.09 million km away from the sun. It has a population of 7.5 billion."
Example 2:
Input: Mars('Mars', '6779 km', '206.6 million km', '0')
Output: "Mars has a size of 6779 km and is 206.6 million km away from the sun. It has a population of 0."
Implement a unique method details in each of the child classes that returns the respective details of the planet.
class Planet:
def __init__(self, name, size, distance_from_sun):
self.name = name
self.size = size
self.distance_from_sun = distance_from_sun
class Earth(Planet):
def __init__(self, name, size, distance_from_sun, population):
super().__init__(name, size, distance_from_sun)
self.population = population
def details(self):
return f"{self.name} has a size of {self.size} and is {self.distance_from_sun} away from the sun. It has a population of {self.population}."
class Mars(Planet):
def __init__(self, name, size, distance_from_sun, population):
super().__init__(name, size, distance_from_sun)
self.population = population
def details(self):
return f"{self.name} has a size of {self.size} and is {self.distance_from_sun} away from the sun. It has a population of {self.population}."
# Test the classes
planet1 = Earth('Earth', '12742 km', '147.09 million km', '7.5 billion')
print(planet1.details())
planet2 = Mars('Mars', '6779 km', '206.6 million km', '0')
print(planet2.details())
Unlock AI & Data Science treasures. Log in!