Python ProgrammingPython Programming

Program to demonstrate an example how inheritance can be used

Inheritance involves a superclass and a subclass. The superclass is the general class and the subclass is the specialized class. A better approach would be to write an Mobile superclass to hold all the general data
about an mobile phones and then write subclasses for each specific company mobiles.
mobile.py
##
# The Mobile class holds general data about various mobiles.


class Mobile:

    # The __init__ method accepts arguments for
    # ram, display, price, camera and
    # battery.

    def __init__(self, ram, display, price, camera, battery):
        self.__ram = ram
        self.__display = display
        self.__price = price
        self.__camera = camera
        self.__battery = battery

    # The following methods are mutators for
    # the class's data attributes.

    def set_ram(self, ram):
        self.__ram = ram

    def set_display(self, display):
        self.__display = display

    def set_price(self, price):
        self.__price = price

    def set_camera(self, camera):
        self.__camera = camera

    def set_battery(self, battery):
        self.__battery = battery

    # The following methods are accessors for
    # the class's data attributes.

    def get_ram(self):
        return self.__ram

    def get_display(self):
        return self.__display

    def get_price(self):
        return self.__price

    def get_camera(self):
        return self.__camera

    def get_battery(self):
        return self.__battery


# The Samsung class represents a mobile. It is a subclass
# of the Mobile class.
class Samsung(Mobile):

    def __init__(self, ram, display, price, camera, battery, performance):
        # Call the superclass's __init__ method and pass
        # the required arguments.
        Mobile.__init__(self, ram, display, price, camera, battery)
        # Initialize the __performance data attribute.
        self.__performance = performance

    def set_performance(self, performance):
        self.__performance = performance

    def get_performance(self):
        return self.__performance


# The Apple class represents a mobile. It is a subclass
# of the Mobile class.
class Apple(Mobile):

    def __init__(self, ram, display, price, camera, battery, speciality):
        # Call the superclass's __init__ method and pass
        # the required arguments.
        Mobile.__init__(self, ram, display, price, camera, battery)
        # Initialize the __speciality data attribute.
        self.__speciality = speciality

    def set_speciality(self, speciality):
        self.__speciality = speciality

    def get_speciality(self):
        return self.__speciality

mobilephone.py
##
# Python's program to calculate Gross Salary using OOPS.

# This program demonstrates the mobile class.

import mobile


def main():
    try:
        # Get the Basic Salary from user.
        Samsung_Galaxy_J7_Prime = mobile.Samsung("3 GB RAM",
                                                 "5.5 inches (13.97 cm)",
                                                 "$245",
                                                 "13 MP Primary Camera",
                                                 "3300 mAh",
                                                 "Octa Core, 1.6 GHz")

        Apple_iPhone_7_Plus = mobile.Apple("3 GB RAM",
                                           "5.5 inches (13.97 cm)", "$900",
                                           "12 MP + 12 MP Dual Primary Camera",
                                           "2900 mAh",
                                           "Light sensor and Proximity sensor")

        print("\n############# Samsung Galaxy J7 Prime ####################")
        print("RAM : \t\t", Samsung_Galaxy_J7_Prime.get_ram())
        print("Display : \t", Samsung_Galaxy_J7_Prime.get_display())
        print("Price : \t", Samsung_Galaxy_J7_Prime.get_price())
        print("Camera : \t", Samsung_Galaxy_J7_Prime.get_camera())
        print("Battery : \t", Samsung_Galaxy_J7_Prime.get_battery())
        print("Performance : \t", Samsung_Galaxy_J7_Prime.get_performance())
        print("###########################################################\n")

        print("################## Apple iPhone 7 Plus ####################")
        print("RAM : \t\t", Apple_iPhone_7_Plus.get_ram())
        print("Display : \t", Apple_iPhone_7_Plus.get_display())
        print("Price : \t", Apple_iPhone_7_Plus.get_price())
        print("Camera : \t", Apple_iPhone_7_Plus.get_camera())
        print("Battery : \t", Apple_iPhone_7_Plus.get_battery())
        print("Speciality : \t", Apple_iPhone_7_Plus.get_speciality())
        print("###########################################################")

    except ValueError:
        print("Error: There is program in data access.")

main()

Sample output of above program.
C:\Python\programs>pep8 --first mobile.py

C:\Python\programs>pep8 --first mobilephone.py

C:\Python\programs>python mobilephone.py

############# Samsung Galaxy J7 Prime ####################
RAM : 3 GB RAM
Display : 5.5 inches (13.97 cm)
Price : $245
Camera : 13 MP Primary Camera
Battery : 3300 mAh
Performance : Octa Core, 1.6 GHz
###########################################################

################## Apple iPhone 7 Plus ####################
RAM : 3 GB RAM
Display : 5.5 inches (13.97 cm)
Price : $900
Camera : 12 MP + 12 MP Dual Primary Camera
Battery : 2900 mAh
Speciality : Light sensor and Proximity sensor
###########################################################

C:\Python\programs>