Object-Oriented Programming/Polymorphism
Appearance
This lesson introduces polymorphism.
Objectives and Skills
[edit | edit source]Objectives and skills for this lesson include:[1]
- Understand polymorphism
- Extending the functionality in a class after inheriting from a base class, overriding methods in the derived class
- Understand encapsulation
- Creating classes that hide their implementation details while still allowing access to the required functionality through the interface, access modifiers
Readings
[edit | edit source]General
[edit | edit source]- Medium: Polymorphism explained simply!
- Wikipedia: Polymorphism (computer science)
- OnlineTutorialsPoint: Types of Polymorphism and Advantages
Java
[edit | edit source]Python
[edit | edit source]Multimedia
[edit | edit source]General
[edit | edit source]Java
[edit | edit source]Python
[edit | edit source]Examples
[edit | edit source]Square (Polygon) |
---|
+ area: float {read only} |
+ Square(side: float) |
Rectangle (Polygon) |
---|
+ area: float {read only} |
+ Rectangle(length: float, width: float) |
Triangle (Polygon) |
---|
+ area: float {read only} |
+ Triangle(side1: float, side2: float, side3: float) |
Polygon |
---|
+ perimeter: float {read only} |
+ Polygon(sides: [float]) - validate_side(side: float) |
Activities
[edit | edit source]- Enhance the inheritance program from the previous lesson to demonstrate polymorphism. Update the UML class diagrams if necessary. Add a main program that uses each class and demonstrates that each subclass is an instance of more than one class.
- Show property and method results when the subclass is referenced as the base class and when it is referenced as a subclass.
- Update program, class, and method documentation consistent with the documentation standards for your selected programming language.
- Include updated unit tests to your submission.
Lesson Summary
[edit | edit source]- Polymorphism is the ability of an object to take on many forms. When an object is assigned to a class reference and when a method of the object is called, the method in the object’s class is executed. Not the method of the reference class (if the reference is a parent class).[2]
- The most commonly recognized major classes of polymorphism are ad-hoc polymorphism, parametric polymorphism, and subtyping.[3]
- Parametric polymorphism allows a function or a data type to be written generically so that it can handle values uniformly without depending on their type. Parametric polymorphism is a way to make a language more expressive while still maintaining full static type-safety.[4]
- Ad-hoc polymorphism refers to polymorphic functions that can be applied to arguments of different types but behave differently depending on the type of the argument to which they are applied (also known as function overloading or operator overloading).[5]
- Function (method) overloading is the ability to create multiple functions of the same name with different implementations. The specific implementation that is called is based on the context of the call. [6]
- Operator overloading, similar to function overloading, is where operators have different implementations depending on their arguments.[7]
- Subtyping allows a function to be written to take an object of a certain type T, but also work correctly if passed an object that belongs to another type S that is a subtype of T.[8]
- Polymorphism can be distinguished by when the implementation is selected: statically (at compile time) or dynamically (at runtime).[9]
- Method overriding is a language feature of polymorphism that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The implementation in the subclass overrides the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class.[10]
- Static polymorphism executes faster, because there is no dynamic dispatch overhead, but requires additional compiler support. Dynamic polymorphism is more flexible but slower.[11]
Key Terms
[edit | edit source]- ad-hoc polymorphism
- Polymorphic functions that can be applied to arguments of different types but behave differently depending on the type of the argument to which they are applied.[12]
- compile-time polymorphism
- The type of polymorphism that is implemented when the compiler compiles the program.[13]
- parametric polymorphism
- When one or more types are not specified by name but by abstract symbols that can represent any type.[14]
- polymorphism
- The provision of a single interface to entities of different types or the use of a single symbol to represent multiple different types.[15]
- polytypism
- A more general function than a polymorphic function. Although one can provide fixed ad-hoc cases for specific data types, an ad-hoc combinator is absent.[16]
- run-time polymorphism
- A type of polymorphism that is implemented dynamically when a program being executed.[17]
- subtyping
- Allows a function to be written to take an object of a certain type T, but also work correctly if passed an object that belongs to a type S that is a subtype of T according to the Liskov substitution principle.[18]
See Also
[edit | edit source]References
[edit | edit source]- ↑ Microsoft: Software Development Fundamentals Exam Details
- ↑ Medium: Polymorphism explained simply!, Shanika Ediriweera
- ↑ Wikipedia: Polymorphism (computer science)
- ↑ Wikipedia: Polymorphism (computer science)
- ↑ Wikipedia: Polymorphism (computer science)
- ↑ Wikipedia: Function overloading
- ↑ Wikipedia: Operator overloading
- ↑ Wikipedia: Polymorphism (computer science)
- ↑ Wikipedia: Polymorphism (computer science)
- ↑ Wikipedia: Method overriding
- ↑ Wikipedia: Polymorphism (computer science)
- ↑ Wikipedia: Polymorphism (computer science)
- ↑ TutorialsPoint Types of Polymorphism
- ↑ Wikipedia: Polymorphism (computer science)
- ↑ Wikipedia: Polymorphism (computer science)
- ↑ Wikipedia: Polymorphism (computer science)
- ↑ TutorialsPoint Types of Polymorphism
- ↑ Wikipedia: Polymorphism (computer science)