0% found this document useful (0 votes)
3 views15 pages

Understanding Polymorphism in C#

The document explains polymorphism in C#, highlighting its two main types: compile-time polymorphism through method overloading and runtime polymorphism via method overriding and interfaces. It also discusses the use of virtual and override keywords, the concept of abstract classes, and the role of interfaces in defining contracts for classes. Overall, polymorphism enhances code flexibility and extensibility by allowing different class objects to be treated uniformly.

Uploaded by

Kamran Sadin
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)
3 views15 pages

Understanding Polymorphism in C#

The document explains polymorphism in C#, highlighting its two main types: compile-time polymorphism through method overloading and runtime polymorphism via method overriding and interfaces. It also discusses the use of virtual and override keywords, the concept of abstract classes, and the role of interfaces in defining contracts for classes. Overall, polymorphism enhances code flexibility and extensibility by allowing different class objects to be treated uniformly.

Uploaded by

Kamran Sadin
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

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

You might also like