Muhammad Imran Ansari
What is Polymorphism? When do you use Polymorphism? Any real time example
By Muhammad Imran Ansari in .NET on Dec 25 2023
  • Dharmwati
    Apr, 2024 25

    polymorphism is one pilor of tha oops methdology.tha means of polymorphism is one object and more than one form. tha real world example is,suppose one person may be an son,an father,an student,and so on.

    • 0
  • Jayraj Chhaya
    Jan, 2024 12

    Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects of different types to be treated as objects of a common base type. It enables code to be written in a more generic and flexible manner, promoting code reuse and extensibility.

    In Dot Net, polymorphism is achieved through inheritance and method overriding. When a derived class inherits from a base class, it can override the base class’s methods with its own implementation. This allows objects of the derived class to be used interchangeably with objects of the base class, providing a level of abstraction and flexibility.

    Polymorphism is used in various scenarios, such as:

    Method Overriding: When a derived class overrides a method from its base class, it can provide a specialized implementation while still adhering to the base class’s contract. This allows for different behavior based on the actual type of the object.

    Interface Implementation: Interfaces define a contract that classes can implement. By implementing an interface, a class can be treated as an instance of that interface, enabling polymorphic behavior.

    Collections and Generics: Polymorphism is often used when working with collections of objects. By using a common base type or interface, different types of objects can be stored in the same collection, allowing for more flexible and generic code.

    A real-time example of polymorphism can be seen in a banking system. Suppose we have a base class called Account and derived classes like SavingsAccount and CheckingAccount. Each derived class can override the Withdraw method from the base class to provide its own implementation. When processing transactions, the system can treat all accounts as instances of the Account class, allowing for polymorphic behavior when withdrawing funds.

    • 0
  • Alpesh Maniya
    Jan, 2024 5

    Polymorphism in programming allows different objects to be treated as instances of the same class but behave differently based on their specific types or implementations. It's used to create more flexible, reusable code and is commonly employed in scenarios like method overriding in inheritance.For instance, think of various shapes (circle, square, triangle) in a drawing application. Even though they're all shapes, each calculates its area differently. Polymorphism allows a single method, like CalculateArea(), to work for all these shapes, providing specific implementations for each.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS