POLYMORPHISM
Once for ever
"It allows objects of different classes to be
treated as objects of a common base class"
SWIP
Kamran Sadin
References are polymorphic. This means a
variable of type x can refer to an object that
subclasses x
SWIP
Kamran Sadin
TWO MAIN TYPES OF POLYMORPHISM IN C#
Compile-time Polymorphism
Method Overloading
This is a form of compile-time polymorphism
where multiple methods in a class have the same
name but different parameter lists. The compiler
selects the appropriate method to call based on
the arguments provided
SWIP
Kamran Sadin
Compile-time Polymorphism
Method Overloading
SWIP
Kamran Sadin
TWO MAIN TYPES OF POLYMORPHISM IN C#
Runtime Polymorphism
Method Overriding & Interfaces
This is the basis of runtime polymorphism.
Inheritance allows a subclass to inherit the
properties and behaviors of its base class.
Method overriding allows a subclass to provide
its own implementation of a method defined in
the base class.
SWIP
Kamran Sadin
Runtime Polymorphism
Method Overriding & Interfaces
SWIP
Kamran Sadin
VIRTUAL AND OVERRIDE
KEYWORDS
The virtual keyword is used in the base class
to indicate that a method can be overridden
by subclasses. The override keyword is used
in the subclass to indicate that the method is
providing a new implementation for the base
class method
SWIP
Kamran Sadin
Virtual and Override
SWIP
Kamran Sadin
ABSTRACT CLASSES
Abstract classes cannot be instantiated on
their own and are meant to be inherited by
subclasses. They often contain abstract
methods (methods without implementation)
that must be overridden by subclasses.
SWIP
Kamran Sadin
Abstract classes
SWIP
Kamran Sadin
INTERFACES
An interface defines a contract that classes
must adhere to. It allows classes to provide
implementations for methods declared in the
interface. A class can implement multiple
interfaces, allowing for a form of multiple
inheritance
SWIP
Kamran Sadin
Interfaces
SWIP
Kamran Sadin
POLYMORPHIC BEHAVIOR
Polymorphism allows you to write code that
works with objects of different classes
without needing to know their specific types.
This makes your code more flexible and
extensible.
SWIP
Kamran Sadin
Polymorphic Behavior
SWIP
Kamran Sadin
I am sure you won't
forget to repost this
MrSadin@[Link]
[Link]
[Link]
Kamran Sadin