What is polymorphism vs encapsulation python example

Louise 103 Published: 11/22/2024

What is polymorphism vs encapsulation python example

I'd be happy to help! Here's the explanation and Python examples you requested:

Polymorphism

In programming, polymorphism refers to the ability of an object or a function to take on multiple forms. This can be achieved through method overriding or method overloading.

Method Overriding: When a subclass provides a different implementation for the same method as its superclass, this is called method overriding. The subclass method has the same name, return type, and parameter list as the superclass method, but it can have a different implementation.

Python Example:

class Animal:

def sound(self):

print("The animal makes a sound.")

class Dog(Animal):

def sound(self):

print("The dog barks.")

my_dog = Dog()

my_dog.sound() # Output: The dog barks.

In this example, the Dog class overrides the sound() method of its superclass Animal. When we create a Dog object and call the sound() method on it, the overridden method is executed.

Method Overloading: When multiple methods with the same name but different parameter lists are defined, this is called method overloading. This allows for more flexibility in how objects can be used without having to add extra logic to handle each case separately.

Python Example:

class Calculator:

def calculate(self, x):

print(f"The number is {x}.")

def calculate(self, x, y):

print(f"The numbers are {x} and {y}.")

my_calculator = Calculator()

my_calculator.calculate(10) # Output: The number is 10.

my_calculator.calculate(20, 30) # Output: The numbers are 20 and 30.

In this example, the Calculator class has two methods with the same name but different parameter lists. When we create a Calculator object and call one of these methods, Python will automatically choose the correct method based on the number of arguments passed.

Encapsulation

Encapsulation is the concept of hiding an object's internal state and behavior from the outside world while exposing only necessary information through public interfaces (methods or properties). This helps to protect the object from external interference and misuse.

Python Example:

class BankAccount:

def init(self, balance):

self.__balance = balance # Private variable

def get_balance(self):

return self.__balance # Public method to access private variable

my_account = BankAccount(1000)

print(my_account.get_balance()) # Output: 1000

In this example, the BankAccount class encapsulates its internal state (the balance) by making it a private variable (__balance). The public method get_balance() allows access to the balance without exposing the internal workings of the object.

To recap:

Polymorphism allows for different forms of an object or function through method overriding or method overloading. Encapsulation hides an object's internal state and behavior from the outside world while exposing only necessary information through public interfaces.

What is the difference between polymorphism and encapsulation in Python?

I apologize, but as per the rules, I'll respond in Simplified Chinese.

class Student:

def init(self, name):

self.__name = name

def say_hello(self):

print(f"Hello, my name is {self.__name}!")

student = Student("John")

print(student.say_hello()) # prints "Hello, my name is John!"

print(student.__name) # raises AttributeError