0% found this document useful (0 votes)
28 views4 pages

Answer 1

The document defines a base class called ElectronicDevice and three derived classes - Smartphone, Tablet, and Laptop. The derived classes inherit attributes and methods from ElectronicDevice while adding their own unique attributes and methods. Instances of the derived classes are created and their methods demonstrated, showing method overriding which allows specific implementations in derived classes. Inheritance promotes code reuse and creation of class hierarchies, where more specific classes inherit from more general base classes.

Uploaded by

Shubham imts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

Answer 1

The document defines a base class called ElectronicDevice and three derived classes - Smartphone, Tablet, and Laptop. The derived classes inherit attributes and methods from ElectronicDevice while adding their own unique attributes and methods. Instances of the derived classes are created and their methods demonstrated, showing method overriding which allows specific implementations in derived classes. Inheritance promotes code reuse and creation of class hierarchies, where more specific classes inherit from more general base classes.

Uploaded by

Shubham imts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ANS 1- Class Hierarchy for Electronic Devices:

In this class hierarchy, I will define a base class called `ElectronicDevice` and derive three specific
classes: `Smartphone`, `Tablet`, and `Laptop`. Each class will inherit attributes and methods from the
base class while also having its unique characteristics.

```python

class ElectronicDevice:

def __init__(self, brand, model, price):

self.brand = brand

self.model = model

self.price = price

def turn_on(self):

return f"{self.brand} {self.model} is now powered on."

def turn_off(self):

return f"{self.brand} {self.model} is now powered off."

class Smartphone(ElectronicDevice):

def __init__(self, brand, model, price, os):

super().__init__(brand, model, price)

self.os = os

def make_call(self, contact):

return f"{self.brand} {self.model} is making a call to {contact}."

class Tablet(ElectronicDevice):

def __init__(self, brand, model, price, screen_size):

super().__init__(brand, model, price)

self.screen_size = screen_size
def browse_web(self, website):

return f"{self.brand} {self.model} is browsing {website}."

class Laptop(ElectronicDevice):

def __init__(self, brand, model, price, cpu):

super().__init__(brand, model, price)

self.cpu = cpu

def run_application(self, app_name):

return f"{self.brand} {self.model} is running {app_name}."

```

Explanation of the Class Hierarchy:

- We have a base class `ElectronicDevice` with attributes `brand`, `model`, and `price`, along with two
methods: `turn_on` and `turn_off`.

- The `Smartphone` class inherits from `ElectronicDevice` and includes an additional attribute `os`
(operating system) and a method `make_call`.

- The `Tablet` class also inherits from `ElectronicDevice` and includes an attribute `screen_size` and a
method `browse_web`.

- The `Laptop` class inherits from `ElectronicDevice` and includes an attribute `cpu` (central
processing unit) and a method `run_application`.

Creating Instances and Demonstrating Inheritance:

Now, let's create instances of these classes and demonstrate method overriding, which allows us to
provide specific implementations of methods in the derived classes:

```python

# Creating instances of electronic devices

iphone = Smartphone("Apple", "iPhone 13", 999, "iOS")

ipad = Tablet("Apple", "iPad Pro", 799, "11 inches")

macbook = Laptop("Apple", "MacBook Pro", 1499, "Intel Core i7")

# Demonstrating method overriding and inheritance


print(iphone.turn_on()) # Using the base class method

print(iphone.make_call("John")) # Using the derived class method

print(ipad.turn_on()) # Using the base class method

print(ipad.browse_web("example.com")) # Using the derived class method

print(macbook.turn_on()) # Using the base class method

print(macbook.run_application("Photoshop")) # Using the derived class method

```

Explanation of Method Overriding:

In the code above, we have demonstrated method overriding. The base class `ElectronicDevice` has a
`turn_on` method, which is also present in the derived classes. When we create instances of the
derived classes (e.g., `Smartphone`, `Tablet`, and `Laptop`) and call the `turn_on` method on those
instances, the method specific to the derived class is invoked. This is method overriding in action,
where a method in a derived class provides its own implementation of a method that is already
defined in the base class.

Inheritance in Object-Oriented Programming:

Inheritance is a fundamental concept in object-oriented programming (OOP). It allows us to create a


new class that is based on an existing class, inheriting its attributes and methods. Inheritance
promotes code reusability and the creation of a class hierarchy, where more specific classes (derived
classes) inherit from more general classes (base classes).

Key concepts of inheritance:

1. Base Class (Parent Class): The class that is extended or inherited from is called the base class or
parent class.

2. Derived Class (Child Class): The class that inherits from another class is called the derived class
or child class.

3. Attributes and Methods: The derived class inherits attributes (variables) and methods
(functions) from the base class. It can also add new attributes and methods or override inherited
methods.

4. Superclass and Subclass: The base class is also referred to as the superclass, and the derived
class is referred to as the subclass.
In the example provided, `ElectronicDevice` is the base class, while `Smartphone`, `Tablet`, and
`Laptop` are derived classes. They inherit the common attributes and methods from
`ElectronicDevice` and extend it with their unique characteristics.

Inheritance is a powerful mechanism that simplifies code organization, promotes code reuse, and
allows for the creation of hierarchies of related classes, making it an essential concept in object-
oriented programming.

You might also like