Object -Oriented
Programming Concepts
Session 9: Polymorphic Variables
OOP SERIES SESSION 9
What Will We Learn Today?
By the end of this session, you'll be able to clearly explain and apply these key concepts:
01 02 03
Polymorphic Variable Types of Polymorphic Variables Reverse Polymorphism
Understand what it means for a variable to Distinguish between Simple Polymorphic Learn how to undo a polymorphic assignment
hold more than one type of object. Variables and Pseudo-variables. and the problems it can cause.
What Is a Polymorphic
Variable?
A polymorphic variable is a variable that can hold a reference to more than one type
of object.
Think of it like a container labeled "Father" that can hold either a Father
object OR a Son object — because Son inherits from Father.
Visualizing a Polymorphic Variable
The diagram above shows variable f1 of type Father. It can point to either a Father object or a Son object — that's polymorphism in action!
The variable f1 sits in the center and can reference either the Father class or the Son class — both have a study() method.
Two Types of Polymorphic Variables
Polymorphic variables come in two forms. Each serves a different purpose in OOP:
Simple Polymorphic Variable Pseudo -variable
A regular variable declared in code that can hold references to An invisible receiver that works inside a method. It is not declared
objects of a parent class or any of its child classes. like a normal variable — it exists automatically.
Simple Polymorphic Variable — The Setup
Let's walk through a C# example step by step. First, we define a base class Emp and two child classes:
class Emp
{
public virtual void display()
{
[Link]("Class Emp");
}
}
// FullTimeEmp inherits Emp
class FullTimeEmp : Emp
{
public override void display()
{
[Link]("Class FullTimeEmp");
}
}
// PartTimeEmp inherits Emp
class PartTimeEmp : Emp
{
public override void display()
{
[Link]("Class PartTimeEmp");
}
}
Simple Polymorphic Variable — In Action
Now we use the polymorphic variable. Notice that e1 is declared as type Emp, but it holds a FullTimeEmp object:
static void Main()
{
Emp e1 = new FullTimeEmp(); // polymorphic variable e1
[Link](); // calls FullTimeEmp's display()
}
Output Why?
Class FullTimeEmp Even though e1 is of type Emp, it runs the overridden method from
FullTimeEmp. This is runtime polymorphism.
Understanding the Simple Polymorphic Variable
Here's a simple way to think about what just happened:
Assign
Declare Emp Call display()
FullTimeEmp
The key insight: the variable type is the parent, but the actual object is the child. C# calls the correct method based on the actual object at runtime.
What Is a Pseudo -variable?
A pseudo-variable is an invisible receiver that works inside a method. Unlike normal
variables, you don't declare it — it exists automatically.
Its main job: eliminate the need to give different names to method
parameters vs. actual parameters, resolving name ambiguity
automatically.
Pseudo -variable Names Across Languages
The pseudo-variable concept exists in every major OOP language — it just has different names:
C++, C#, Java Smalltalk Eiffel
Called this — refers to the current object Called self — same concept, different Called current — refers to the current
inside a method. syntax. instance of the class.
What Is Reverse
Polymorphism?
Reverse polymorphism is the process of undoing a polymorphic assignment —
casting a parent-type variable back to its original child type.
Emp e1 = new FullTimeEmp(); // assign child to parent
type
FullTimeEmp e2 = (FullTimeEmp)e1; // cast back to child
type
Problems With Reverse Polymorphism
Reverse polymorphism can be tricky. Two key problems can arise when casting back to a child type:
Problem of Identification Problem of Assignment
How do you know what the actual type of the object is? If a1 could Even if you identify the type, assigning it incorrectly (e.g., casting a
be a Mammal or a Reptile, casting to the wrong type causes an Mammal object to Reptile) will throw a runtime exception.
error.
Reverse Polymorphism —
Visual Example
The diagram below shows the inheritance hierarchy and the confusion that arises in
reverse polymorphism:
Variable a1 of type Animal could hold a Mammal or Reptile. When reversing, the
program must figure out which child type it actually is — that's the identification
problem.
Session 9 — Summary
Here's everything you learned in this session at a glance:
Polymorphic Variable Pseudo -variable Reverse Polymorphism
A variable that can hold references to An invisible receiver inside a method Undoing a polymorphic assignment by
objects of more than one type (parent (this, self, or current) that casting back to the child type — watch
or child class). resolves name ambiguity out for identification and assignment
automatically. problems.